30
loading...
This website collects cookies to deliver better user experience
1. > node -v
1. > npm -v
/* app.js file */
console.log("Node is working");
E:\> node -v
v14.16.0
E:\> npm -v
v6.14.11
E:\> node app.js
Node is working
E:\>
1 > npm install
package.json
in root directory to install all dependencies defined in the file.{
"name": "ApplicationName",
"version": "0.0.1",
"description": "Application Description",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"repository": {
"type": "git",
"url": "https://github.com/npm/npm.git"
},
"dependencies": {
"express": "~3.0.1",
"sequelize": "latest",
"q": "latest",
"tedious": "latest",
"angular": "latest",
"angular-ui-router": "~0.2.11",
"path": "latest",
"dat-gui": "latest"
}
}
{
"repository": {
"type": "git",
"url": "https://github.com/npm/npm.git"
}
}
npm docs
command will be able to find you.{
"scripts": {
"start": "node app.js"
}
}
1 "start": "node app.js"
{
"dependencies": {
"express": "~3.0.1",
"sequelize":"latest",
"q":"latest",
"tedious":"latest",
"angular":"latest",
"angular-ui-router": "~0.2.11",
"path":"latest",
"dat-gui":"latest"
}
}
latest
version of a file, you just have to put latest in place of the version name./*app.js*/
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, function() {
console.log('Server running at http://'+ hostname + ':' + port + '/');
})
http
to create an http server we use require('http')
and pass it to a variable named http
1 var http = require('http');
localHost
i.e 127.0.0.1
and port
number 3000
and assign this to the variables hostname
and port
, respectively.var hostname = '127.0.0.1';
var port = 3000;
createServer
method.var server = http.createServer(function(req, res){
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
statusCode: 200
, Content-Type
header of plain text and and ends with the string Hello World
. This is the response that the server can send to browser.req
and res
which is the request
from and response
to the server, respectively.server.listen(port, hostname, function() {
console.log('Server running at http://'+ hostname + ':' + port + '/');
});
node app
E:\ >node app.js
Server running at http://127.0.0.1:3000/