To create HTTP Server with Node.js is very simple. You don't need the applications like Apache Server or other.
For this tutorial we can use "express" module to create the HTTP Server.
Install express module:
$ npm install express --save
Defenitions:
Routing:
Listen:
Example:
Save and run the script
After running, you can see the server running on url http://127.0.0.1:3000/
var express = require('express')
var app = express();
var port = 3000;
app.get('/', function (req, res) {
res.send('Learn Node.js')
})
app.listen(port, function(err, res){
console.log("Listen on port : " + port);
})
Example:
var express = require('express')
var app = express();
var port = 3000;
app.get('/', function (req, res) {
res.send('Learn Node.js')
})
app.listen(port, function(err, res){
console.log("Listen on port : " + port);
})
Save and run the script
$ node <nama file>