IV
IV
What is Express?
Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and
add helpful new features.
It makes it easier to organize your application’s functionality with middle ware and routing; it adds helpful
utilities to Node.js’s HTTP objects;it facilitates the rendering of dynamic HTTP objects.
Express is a part of MEAN stack, a full stack JavaScript solution used in building fast, robust, and
maintainable production web applications.
• MongoDB(Database)
• ExpressJS(Web Framework)
• AngularJS(Front-end Framework)
• NodeJS(Application Server)
• Express is a lightweight module that wraps the functionality of the Node.js http module.
• Express also extends the functionality to handle server routes, responses, cookies, and statuses of
HTTP requests.
Steps included in implementing express
• Installing express
• Configuring setting
• Starting the express server
Getting Started with Express
• Add the express module using the following command from the root of your project:
npm install express
• Also add express to your package.json module to ensure that express is installed when you deploy
your application.
• Once you have installed express, you need to create an instance of the express class to act as the
HTTP server for your Node.js application.
• The following lines of code import the express module and create an instance of express that you
can use:
var express = require('express’);
var app = express();
Configuring Express Settings
Express provides several application settings that control the behavior of the Express server.
The express object provides the set(setting, value) and enable(setting) and disable(setting)
methods to set the value of the application settings.
To get the value of a setting, you can use the get(setting), enabled(setting), and disabled(setting) methods.
For example:
app.enabled('trust proxy'); \\true
app.disabled('strict routing'); \\true
app.get('view engine'); \\pug
Starting the Express Server
• To begin implementing Express as the HTTP server for your Node.js application, you need to
create an instance and begin listening on a port.
var express = require('express’);
var app = express();
app.listen(8080);
• The app.listen(port) call binds the HTTP connection to the port and begins listening on it.
• In fact, the value returned by express() is actually a callback function that maps to the callback
function that is passed into the http.createServer() and https.createServer() methods.
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
host: '127.0.0.1’,
key: fs.readFileSync('ssl/server.key’),
cert: fs.readFileSync('ssl/server.crt')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
app.get('/', function(req, res){
res.send('Hello from Express');
});
4.4 Typescript
TypeScript is a superset of JavaScript that adds static typing, interfaces, and advanced features to
the language. In Angular applications, TypeScript enhances code quality and maintainability by providing
strong typing, improved tooling, better error detection during development, and support for object-
oriented programming concepts, leading to more robust and scalable applications.
A TypeScript class is defined using the class keyword, followed by the class name and a body
containing properties and methods. For example:
class Car {
constructor(public brand: string, public model: string) {}
drive() {
console.log(`Driving a ${this.brand} ${this.model}`);
}
}
An interface in TypeScript defines the structure of an object, specifying what properties and
methods it should have. Interfaces are useful for type-checking and ensuring that classes or objects adhere
to a specific contract.
Example of an Interface
interface Vehicle {
brand: string; // Property
model: string; // Property
year: number; // Property
displayInfo(): string; // Method
}
displayInfo(): string {
return `${this.brand} ${this.model}, Year: ${this.year}`;
}
}
console.log(cube(3)); // Output: 27
4. Function Overloading:
TypeScript allows function overloading, where you can define multiple signatures for a single function.
function multiply(x: number, y: number): number;
function multiply(x: string, y: string): string;
function multiply(x: any, y: any): any {
return x * y;
}