📄 Table of Contents

Inbuilt modules in nodejs

const http = require("http"); const fs = require("fs");const path= require("path");const os = require("os");const EventEmitter = require("events");
const event = new EventEmitter();
There is a very good module for working with file uploads, called "Formidable".The Nodemailer module makes it easy to send emails from your computer.above both can be downloaded & installed using npm.fs.createReadStream("input.txt"); // to read one by one in streaming fashion

Create Your Own Modules

Example

exports.myDateTime = function () {
return Date();
};

Include Your Own Module

Example

var http = require(‘http’);
var dt = require(‘./myfirstmodule’);
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/html’});
res.write(“The date and time are currently: “ + dt.myDateTime());
res.end();
}).listen(8080);
const sample = require('./sample.js');// if sample.js provided a function
sample();
// if sample.js provided a variable
console.log(sample);
// etc
// built-in module used for creating HTTP servers
const http = require('http');
// third-party package used for making HTTP requests
const axios = require('axios');
// sample.js
const sample = 'sample variable string';
const sample2 = 'sample2 variable string';
module.exports = sample;// importing module
const sample = require('./sample.js');
console.log(sample); // => sample variable string
console.log(sample2); // => Reference error
// sample.js
const sample = 'sample variable string';
exports.sample = sample;
// importing with ES Modules
import sample from './sample.mjs';
// within a CommonJS module
// by default .js extension
console.log(__filename, __dirname); // double underscore

Route handling in nodejs

const http = require("http");const server = http.createServer((req, res) => {if (req.url === "/") {console.log("=1 ===");res.end("hello @ home page"); // send raw data}else if (req.url === "/about") {console.log("=== 2 ===");res.end("hello @ about page");}else {
console.log("333");
//send html data
res.writeHead(404, { "Content-type": "text/html" });
//send html data
res.end("<h1> 404 error codes. Page doesn't exist </h1>");
}});

create simple API in nodejs

EXPRESS JS

how to serve STATIC html page in express.js

const path = require("path");
const staticPath = path.join(__dirname.join("../public");
app.use(express.static(staticPath);

how to serve DYNAMIC html page in express.js

const hbs = require("hbs");app.set("view engine", "hbs"); // make view engine use handlebars.res.render("index");  // this refers to index of views folder.NOTE: res.render and NOT res.send

Can we customize “views” directory

const templatePath = path.join(__dirname.join("../templates");
app.set(“views”, templatePath);

Partials in Express JS

const hbs = require("hbs");const partialsPath = path.join(__dirname, '../templates/partials');hbs.registerPartials(partialsPath);

how to render Partials

how to make server recognise any change in either .js or .hbs file on run time

nodemon index.js -e js, hbs

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.