Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open webpage when start nodejs server [duplicate]

I was wondering if and how i can automatically open a webpage when i start my server with nodejs

app.listen(app.get("PORT"), () => {
        console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
    })

I have this basic example where when i run node start proyect it will start the server and then i manually need to click on the console logged message or open a browser and type bla bla bla

I want to raise that web for itself like when in React we run the command npm run start

I tried to google it but i dont know how to search for this because i dont get any usefull info. Thank u very much

like image 336
Programmer89 Avatar asked Jan 30 '26 06:01

Programmer89


1 Answers

Use open to Open stuff like URLs, files, executables. Cross-platform.

const open = require('open');

// opens the url in the default browser 
open('http://stackoverflow.com');

UPDATE

open simplifies cross platform functionality, in node you can have access to the platform by process.platform and write platform specific executions

for example here is the native version in mac without using any NPM module

const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);

I don't have access to a windows system to write the code for windows

like image 153
Robot Avatar answered Feb 01 '26 01:02

Robot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!