1const express = require('express')
2const app = express()
3const port = 3000
4
5app.get('/', (req, res) => res.send('Hello World!'))
6
7app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
1// Run a command asynchronously
2const { spawn } = require('child_process');
3const dir = spawn('cmd', ['/c', 'dir']);
4
5dir.stdout.on('data', data => console.log(`Stdout: ${data}`));
6dir.stderr.on('data', data => console.log(`Stderr: ${data}`));
7dir.on('close', code => console.log(`Exit code: ${code}`));
8
9// Run a command synchronously
10const { spawnSync } = require( 'child_process' );
11const dir = spawnSync('cmd', ['/c', 'dir']);
12
13console.log(`Stdout: ${dir.stdout.toString()}`);
14console.log(`Stderr: ${dir.stderr.toString()}`);
1#1. server.js
2var express = require('express');
3var app = express();
4
5app.get('/', function (req, res) {
6 res.status(200).send('Hello World!');
7});
8
9var port = process.env.PORT || 3000;
10
11var server = app.listen(port, function() {
12 console.log('Express server listening on port ' + port);
13});
14
15open cmd run server.js
16"node server.js"
17log:Express server listening on port 3000
18
19& then
20
21open link "http://localhost:3000/" in your browser and show result.
1Save your .js file in the same folder as node.js,
2For windows , Usually location is- "C:\Program Files\nodejs"
3Open 'Command prompt and locate node.js folder' OR directly open 'Node.js command prompt' and type
4>> node (file_name).js