DEV Community

loading...

How to build a custom server with nodejs in 3 min

vkassingh profile image Vikas Singh ・1 min read

Hello people!
Hope you like my previous HOW TO tutorials. So in today's post, we will be making a custom server with Nodejs in 3 minutes.

What you need:
* Knowledge of javascript and Nodejs
* Npm installed

So without any further due, let us get to the main point.
If you have installed the npm package, then Go to the terminal!

//switch to the directory where you want to store your files
cd documents

//make a new folder to store the meta data of the server.
mkdir server

//switch to this "server" file and then start with npm
cd server

//create a package.json file with npm
npm init

//use this command to create a file named app.js
touch app.js

NOTE: we will code only in the app.js folder.

Create a custom server with Nodejs - Vikas Singh

You should get this message(listening at port 3000) on the console if your server is working properly.

Congrats, you have your own custom server.

Give a ❤ if you liked the tutorial.

Discussion

pic
Editor guide
Collapse
aminnairi profile image
Amin

Hi Vikas and thanks for this article!

For people that are on the clock, you can even create a server in one terse line.

require("http").createServer((_, r) => r.end("Node.js")).listen(8080);
Enter fullscreen mode Exit fullscreen mode

You can even fire a Node.js server in a terminal.

$ node -e "require('http').createServer((_, r) => r.end('Node.js')).listen(8080)"
Enter fullscreen mode Exit fullscreen mode

This is not very interesting for most applications, but this is a cool trick to know!

Collapse
vkassingh profile image
Vikas Singh Author

thanks for sharing.