3

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

Programmer89
  • 145
  • 3
  • 15

1 Answers1

6

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

Robot
  • 913
  • 6
  • 8
  • 1
    Jesus, so simple ahahah. Thank u a LOT. Anyway, is there a native way for doing this with js vanilla? or nodejs native modules? – Programmer89 Apr 22 '21 at 22:52
  • 1
    @Programmer89 you're welcome :), I added answer of your comment's question in the post – Robot Apr 22 '21 at 23:16