Learn Node.js - How To Create Simple HTTP Server with Node.js

0

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:
var express = require('express')
var app = express();
var port = 3000;

Routing:
app.get('/', function (req, res) {
  res.send('Learn Node.js')
})

Listen:
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>

After running, you can see the server running on url http://127.0.0.1:3000/ 
Tags

Post a Comment

0Comments
Post a Comment (0)