August, 2019
So we installed NodeJS on our machine.
Now we want to learn how to create a simple server using the HTTP module.
index.js
:touch index.js
// import http module
const http = require("http");
// server configuration
const HOST = "127.0.0.1";
const PORT = 8080;
// create the server
const server = http.createServer((req, res) => {
res.end("Hello!");
});
// make the server listen to requests
server.listen(PORT, HOST, () => {
console.log(`Server running at: http://${HOST}:${PORT}/`);
});
Note: This is an extremely simple server. I would recommend you to read the docs of the HTTP module, especially how headers work and how to send them.
node index.js
Server running at: http://127.0.0.1:8080/
Now you can click on the link and reach your created server.
HTTP/HTTPS
module or some libraries like express? Why do you use it?Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.