node js server
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
node js server
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
node run command
// Run a command asynchronously
const { spawn } = require('child_process');
const dir = spawn('cmd', ['/c', 'dir']);
dir.stdout.on('data', data => console.log(`Stdout: ${data}`));
dir.stderr.on('data', data => console.log(`Stderr: ${data}`));
dir.on('close', code => console.log(`Exit code: ${code}`));
// Run a command synchronously
const { spawnSync } = require( 'child_process' );
const dir = spawnSync('cmd', ['/c', 'dir']);
console.log(`Stdout: ${dir.stdout.toString()}`);
console.log(`Stderr: ${dir.stderr.toString()}`);
run node js
node name_app.js
create node project
#1. server.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.status(200).send('Hello World!');
});
var port = process.env.PORT || 3000;
var server = app.listen(port, function() {
console.log('Express server listening on port ' + port);
});
open cmd run server.js
"node server.js"
log:Express server listening on port 3000
& then
open link "http://localhost:3000/" in your browser and show result.
how to run a js file in node
Save your .js file in the same folder as node.js,
For windows , Usually location is- "C:\Program Files\nodejs"
Open 'Command prompt and locate node.js folder' OR directly open 'Node.js command prompt' and type
>> node (file_name).js
node command
> node
Welcome to Node.js <version>.
>'type in javascript'
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us