DEV Community

Jamiebones
Jamiebones

Posted on

HOW TO CREATE A NODEJS SERVER WITHOUT USING A FRAMEWORK

NodeJS is a Javascript runtime built on Chrome's V8 Javascript engine that allows
programmers run Javascript on the backend. Creating a *http server to respond to http request in NodeJS is quite simple and straight forward.
In this short tutorial we are going to learn how to create our own http server without using a framework or library like ExpressJS.
NodeJS comes bundled with all that we need to run an http server. To run or developed a Node application, Node must be installed in your computer. You can download Node here. If you have node installed, let look at some code.

 const http = require("http");
 const server = http.createServer(function(req,res){
 res.end("Hello world");
});
server.listen(3000, function(){
  console.log("server listening on port 3000");
});
Enter fullscreen mode Exit fullscreen mode

We required and stored the http module baked in node in a variable called http. The method createServer is called on the http object we just required. A server is created and a response is sent to the client that simply say Hello world.
Our server can be started by running the file the above code is contained. The server runs on port 3000 and to visit our simple server we can make a request to http:localhost:3000 from the browser or from a terminal using curl. By running this simple code we have successfully created an http web server.

Discussion (0)