Node.js provides a low level HTTP API that enables programmers to build HTTP applications.

Here’s a simple web server

var http = require('http');

var httpServer = http.createServer(function(req, res){
    res.writeHead(200, {'Content-type': 'text/html'});
    res.end("Okay");
});

httpServer.listen('9000');

http.CreateServer returns a new instance of http.Server and is used to create a TCP or local server.

httpServer.listen starts a local socket server listening for connections on the given path.

EXPRESS

Using the web framework Express can speed up your development drastically.

Other benefits of using Express are routing, middleware, template engines and more.

To begin using Express start by creating a package.json file. This file includes your dependencies, app version, and more.

Running the following command creates it automatically

npm init

To install Express run

npm install --save express

Then create a web server

// index.js
var express = require('express');
var app = express();

app.get('/', function(req, res){
    res.send('Hello there!');    
}).listen(3000);

Instead of using app to handle routing we’ll create an express.Router that way middleware will only apply to the router and not the whole app.

// router.js
var express = require('express');
var router = express.Router();

// middleware called before serving requests 
router.use(function(req, res, next){
    console.log(req.headers);
    next();
});

router.get('/v1', function(req, res){
    res.send('My API');
});

module.exports = router;

Use the router inside index.js

// index.js
var express = require('express');
var app = express();

var router = require('./router');

// requests to /api are handled by router
app.use('/api', router);

app.get('/', function(req, res){
    res.send('Hello there!');    
}).listen(3000);

Go to localhost:3000/api/v1 and you will see the HTTP header you sent to the router’s middleware.

Now that our app has an api we should serve proper html with css.

Create a public folder with main.css inside

/* public/main.css */

body {
	background-color: red;
}

And serve it as static content

// index.js
var express = require('express');
var app = express();

var router = require('./router');

// express will look in this folder for static content 
app.use(express.static('public'));

// requests to /api are handled by router
app.use('/api', router);

app.get('/', function(req, res){
    res.send('Hello there!');    
}).listen(3000);

Create an index.html page inside the public directory

<!--public/index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Express App</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <h1>Express is awesome!</h1>
</body>
</html>

Lastly remove app.get(‘/’) because index.html is served statically.

// index.js
var express = require('express');
var app = express();

var router = require('./router');

// express will look in this folder for static content 
app.use(express.static('public'));

// requests to /api are handled by router
app.use('/api', router);

app.listen(3000);

Run index.js

node index.js

Express Nodejs

And that’s the basics of Express. Of course, there’s much more such as templates to be learned.

Thanks for reading!