Setting up HTTP Server
HTTP-----Hypertext Transfer Protocol
HTTPS----Hyper Text Transfer Protocol Secure.
500 error is server side error
400 error is user side error
200 show working good.
100 show still requesting and going on and process...
Create a simple Express server
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (request,response) => {
response.send("Hello Express :)");
});
app.listen(port, ()=> {
console.log(`Express server listing on port ${port}`);
})
Create a simple server
const http = require('http'); // http --> node's core module
const listener = function(req, res){
console.log("our server is working.")
res.writeHead(200); // 400 is bad request
res.end("Our server is live")
}
const simpleServer = http.createServer(listener);
simpleServer.listen(8080);
Sending JSON data from the server
// sending JSON data from the server
const http = require('http'); // http --> node's core module
const listener = function(req, res){
console.log("our server is working.")
res.setHeader("Content-Type", "text/json");
res.writeHead(200); // 400 is bad request
res.end(`{"message" : "This is a JSON Object from our server.}`)
}
const simpleServer = http.createServer(listener);
simpleServer.listen(8080);
sending CSV data from the server
// sending CSV data from the server
const http = require('http'); // http --> node's core module
const listener = function(req, res){
console.log("our server is working.")
res.setHeader("Content-Disposition", "attachment; filename=myDetails.csv");
res.writeHead(200); // 400 is bad request
res.end(`name, school,age\n Dolby, DPS,21`)
}
const simpleServer = http.createServer(listener);
simpleServer.listen(8080);
now create your own server. (give it a try amy)
simple server 2 (second class)
HTTP server (using multiple routes)
const http = require('http');
const listener = function(req,res){
res.setHeader("Content-Type",'text/html');
console.log("Server is working");
res.writeHead(200);
// creating multiple routes using switch case below
switch(req.url){
case "/":
res.end("Http server response: Home Page");
break;
case "/1":
res.end("Http server response: student 1");
break;
case "/2":
res.end("Http server response: student 2");
break;
case "/name":
res.end("Http server response: Sanjeet");
break;
case "/company":
res.end("Http server response: MNC");
break;
default:
res.end("<h1>Http server response: Incorrect endpoint.<br><a href = 'http://localhost:3000/name'>Go Here</a></h1>")
}
}
const simpleServer = http.createServer(listener);
simpleServer.listen(3000);
express.js -- it is a node js frame work.
Basic Express server
const expressjs = require('express');
const expressAp = expressjs();
expressAp.get('/', function (req,res){
res.send("This is express server");
})
expressAp.listen(3000);
intermediate Express server (using multiple routes)
const expressjs = require('express');
const expressAp = expressjs();
expressAp.get('/', function (req,res){
res.send("This is express server");
});
expressAp.get('/name', function (req,res){
res.send("Http server response: Sanjeet");
});
expressAp.get('/company', function (req,res){
res.send("Http server response: MNC");
});
expressAp.listen(3000);
// expressAp.listen(3000, function (){
// console.log("server is runing on port 8484.")
// });
intermediate Express server 2 (using multiple routes)
const expressjs = require('express');
const expressAp = expressjs();
expressAp.get('/', function (req,res){
res.send("Hello World");
});
expressAp.get(/^\/students\/(\d+)$/ , function (req,res){
res.send("ExpressJs backed http endpoint student id" + req.params[0]);
});
// expressAp.get('/students/:studentId', function (req,res){
// res.send("ExpressJs backed http endpoint student id " + req.params.studentId);
// });
expressAp.get('/students/:studentId/region', function (req,res){
res.send("ExpressJs backed http endpoint student id " + req.params.studentId + req.params.region);
});
// expressAp.get('/students/2', function (req,res){
// res.send("ExpressJs backed http endpoint student id 2");
// });
expressAp.listen(8000, function (){
console.log("server is running on port 8000.")
});
Express file Transfer
const expressjs = require('express');
const fs = require('fs');
const path = require('path');
const expressAp = expressjs();
expressAp.use(function(req,res,next){
let filePath = path.join(__dirname,req.url)
fs.stat(filePath, function(err,fileInfo){
if(err){
next();
return;
}
if(fileInfo.isFile()){
res.sendFile(filePath);
}
else{
next();
}
});
});
expressAp.listen(3000, function (){
console.log("server is runing on port 3000.")
});
Promises are not asynchronous..
Question-> Let us try to create a web server on port 8448 instead of 8080.
Solution :
Question -> Let us try to return a 400 error message!
Solution :

MCQ
If we have to update our password, what HTTP method should we use ?
GET
PUT
POST
UPDATE
If there is a Null pointer exception in server code, what response should it return?
404
302
500
201
Which of the following methods has no message body?
POST
GET
PUT
DELETE
Find incorrect mapping.
200 OK
400 Bad Request
402 Not Found
301 Moved Permanently
If we have to configure a RESTFull url, for searching a book given its id, what will the request look like.
GET /{id}/books/
GET /{Id}
GET /Books/{id}
GET /Books?id={id1}
Express is flexible ______ web application framework .
Node.js
React.js
Angular.js
Golang
Which of the following express.js features .
Allows to create middlewares
AllDefines routing tables
None of these
Both a and b
What function arguments are available to Express Route handlers .
Request Object
Response Object
Next()
All of these
What middleware do we use for custom logging in express.js .
Morgan
Cors
Logger
Log4j
What does [^abc] regex represent .
Any character without a, b and c
Any character without a, but has b and c
Any character with a, b and c
List of characters without a, b and c