Skip to main content

Create Simple WebServer in 2 min with Node Js



✊Let's create a simple web server  on node Js, which responds on Get method.

Step: 1

Install Node if you haven't yet according to you os platform.

Install npm globally.

npm install -g 


Verify the version of node and npm to check proper installations.



Step: 2

Open your favourite editor, am using vs code, and create "server.js" file.


Step: 3

We need http module to create http web server, so require the module as below.

const { createServer } = require('http');
const PORT = '3080' | process.env.PORT;


createServer((req, res) => {

res.end('welcome to node server!')


}).listen(PORT, () => {
console.log('server running on port 3080')
})


Step: 4

Create dynamic Port value from the process or static value if it runs in local env and listen the server on that port using listen method.

Step: 5

Create server instance as "createServer" and handle the req and responses as you like in the braces block.

Step: 6

Run node server as , node server.js in your terminal.



And you are done! ✌ Give a hit using Curl to get some response from the server you have create now..


It gives you a quick start to explore more on server rendering...




Comments