- NodeJS scenario is module based.
- basic-server.js content:
- // NOT PART of EXAM, ONLY FOR KNOWLEDGE
- const http = require('http');
- const router = (req, res)=>{
- console.log('${req.method} ${req.url}')
- res.writeHead(200, {'Content-Type':'text/html'}); // 200 means OK response, the type of response is text/html
- res.end('<h1>Hello World</h1>');
- }
- const routes = {
- 'GET':{
- '/':(req,res)=>{
- res.writeHead(200, {'Content-Type':'text/html'}); // 200 means OK response, the type of response is text/html
- // res.end('<h1>Hello World</h1>');
- res.end(`<form method ="post">
- <input type="text" name="username" placeholder="username">
- <br>
- <input type="password" name="password" placeholder="password">
- <br>
- <input type="submit" value="Login">
- </form>`);
- },
- 'about':(req,res)=>{
- res.writeHead(200, {'Content-Type':'text/html'}); // 200 means OK response, the type of response is text/html
- res.end('<h1>About Us</h1>');
- }
- },
- 'POST':{ '/':(req,res)=>{
- let body = '';
- req.on('data', (data)=>
- {
- body+=data;
- });
- req.on
- }
- },
- '':()=>{}
- }
- // For Each is just for iteration, does not return any value
- const router = (req, res)=>{
- console.log('${req.method} ${req.url}')
- let resolveRoute = routes[req.method][req.url]
- if (resolveRoute !== undefined){
- resolveRoute(req,res);
- } else{
- routes['NA'](req, res)
- }
- }
- end of content
- http
- .createServer((req, res)=>{
- res.writeHead(200, {'Content-Type':'text/html'}); // 200 means OK response, the type of response is text/html
- res.end('<h1>Hello World</h1>');
- })
- .listen(3000, ()=> console.log('Server is running on http://localhost:3000')); // running on 3000 port
- // NodeMon is used to sync the code, this will sync any changes whenever the code is changed so it does not require any kind of restart.
- // GET and POST
- // GET will receive
- // POST will send the response
- There are different types of modules
- Dependency Module
- Non-dependency Module (Does not do anything to the code, but provide support related to code such as NodeMon, monitors the code and syncs instantly
- creating a module first
- package.json file is created automatically
- index.js - a module file
- content:
- constant express = require('express');
- const app = express();
- const port = 4000;
- const logger = (req, res, next)=>{ // Next shows that it is a middleware, next means the next middleware to be executed.
- req.msg = '/nThis message is from middleware!!!');
- next();
- }else{
- res.send("Access denied")
- }
- }
- const auth = (req, res, next)=>{
- if (req.query.name === "Peter"){ // Query means GET, BODY means POST
- next()
- }
- }
- app.use(logger); // To use the middleware, we have to use this.
- app.get('/', (req, res)=>{
- res.send('<h3>Hello World</h3> ${msg}');
- })
- app.get('/users', auth, (req, res)=>{
- res.send('<h3>Users Route</h3> ${msg}');
- })
- app.listen(port, ()=> console.log('Server is running on http://localhost:${port});
- // There are different template engines
- // The controller decides the route to go.
Recent Pastes