NodeJS

Build your first Http Server in Node.js

posted by tungnt 01/08/2016 0 comments

In this tutorial we will be talking about how we can set up a simple Node.js HTTP server from a beginner’s perspective. Node is a fantastic candidate for creating web servers; which are lightweight and can handle a great number of simultaneous requests. This means if you’re interested in learning to build scalable web applications this is a great beginner’s guide.

Node.js is shipped with several core modules out of the box, which we will be using to set up our own http server. The http module makes it simple to create an http server via its simple but powerful api. Lets create an empty file named server.js and write the following code into it:

var http = require('http');
const PORT = 8080;
var server = http.createServer(function (req, res) {
    res.end("It's works");
});

server.listen(PORT, function () {
    console.log("Server listening on: http://localhost:%s", PORT);
});

Type:

node server.js

Output:

Server listening on: http://localhost:8080
Bình luận
989 Total Views 2 Views Today

You may also like