How to Use Nodejs to Open Default Browser and Navigate to a Specific Url

How to start Node.js in specific URL?

node server.js & curl 'http://localhost/my_page'

This starts the server in the background, then immediately uses curl to make an HTTP request.

The server will still be running after this. You can bring it to the foreground (e.g. in order to kill it) with fg. Alternatively just run the two commands (without &) in separate terminals (or screens).

If the server doesn't start fast enough, just add a delay:

node server.js & sleep 1 ; curl 'http://localhost/my_page'

How to open a browser window from a node-webkit app?

To open a link in the user's default browser, you could use gui.Shell.openExternal("http://website.com"). Checkout the documentation for gui.Shell.

How to open links in default browser using Electron

You can do this the way you tried using webContents's class instance methods instead of static functions

const { app, BrowserWindow, shell } = require('electron')

app.once('ready', () => {
const handleRedirect = (e, url) => {
if (url !== e.sender.getURL()) {
e.preventDefault()
shell.openExternal(url)
}
}
const win = new BrowserWindow()
// Instead bare webContents:
win.webContents.on('will-navigate', handleRedirect)
win.loadURL('http://google.com')
})


Related Topics



Leave a reply



Submit