Skip to content

Instantly share code, notes, and snippets.

View sameralsayed's full-sized avatar

Samer Saeid sameralsayed

View GitHub Profile
@sameralsayed
sameralsayed / Express.js
Created June 16, 2022 20:26
"Express is a fast, unopinionated minimalist web framework for Node.js" - official web site: Expressjs.com
Express.js
"Express is a fast, unopinionated minimalist web framework for Node.js" - official web site: Expressjs.com
Express.js is a web application framework for Node.js. It provides various features that make web application development fast and easy which otherwise takes more time using only Node.js.
Express.js is based on the Node.js middleware module called connect which in turn uses http module. So, any middleware which is based on connect will also work with Express.js.
Express.js
Advantages of Express.js
@sameralsayed
sameralsayed / Frameworks for Node.js
Created June 16, 2022 20:26
You learned that we need to write lots of low level code ourselves to create a web application using Node.js in Node.js web server section.
Frameworks for Node.js
You learned that we need to write lots of low level code ourselves to create a web application using Node.js in Node.js web server section.
There are various third party open-source frameworks available in Node Package Manager which makes Node.js application development faster and easy. You can choose an appropriate framework as per your application requirements.
The following table lists frameworks for Node.js.
Open-Source Framework Description
Express.js Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. This is the most popular framework as of now for Node.js.
Geddy Geddy is a simple, structured web application framework for Node.js based on MVC architecture.
@sameralsayed
sameralsayed / Node.js EventEmitter
Created June 16, 2022 20:25
Node.js allows us to create and handle custom events easily by using events module. Event module includes EventEmitter class which can be used to raise and handle custom events.
Node.js EventEmitter
Node.js allows us to create and handle custom events easily by using events module. Event module includes EventEmitter class which can be used to raise and handle custom events.
The following example demonstrates EventEmitter class for raising and handling a custom event.
Example: Raise and Handle Node.js events Copy
// get the reference of EventEmitter class of events module
var events = require('events');
//create an object of EventEmitter class by using above reference
@sameralsayed
sameralsayed / Node Inspector
Created June 16, 2022 20:24
In this section, we will use node inspector tool to debug a simple Node.js application contained in app.js file.
Node Inspector
In this section, we will use node inspector tool to debug a simple Node.js application contained in app.js file.
app.js Copy
var fs = require('fs');
fs.readFile('test.txt', 'utf8', function (err, data) {
debugger;
@sameralsayed
sameralsayed / Debug Node.js Application
Created June 16, 2022 20:24
In this section, you will learn ways to debug Node.js application.
Debug Node.js Application
In this section, you will learn ways to debug Node.js application.
You can debug Node.js application using various tools including following:
Core Node.js debugger
Node Inspector
Built-in debugger in IDEs
Core Node.js Debugger
Node.js provides built-in non-graphic debugging tool that can be used on all platforms. It provides different commands for debugging Node.js application.
@sameralsayed
sameralsayed / Node.js File System
Created June 16, 2022 20:23
Node.js includes fs module to access physical file system. The fs module is responsible for all the asynchronous or synchronous file I/O operations.
Node.js File System
Node.js includes fs module to access physical file system. The fs module is responsible for all the asynchronous or synchronous file I/O operations.
Let's see some of the common I/O operation examples using fs module.
Reading File
Use fs.readFile() method to read the physical file asynchronously.
Signature:
fs.readFile(fileName [,options], callback)
@sameralsayed
sameralsayed / Node.js Web Server
Created June 16, 2022 20:23
In this section, we will learn how to create a simple Node.js web server and handle HTTP requests.
Node.js Web Server
In this section, we will learn how to create a simple Node.js web server and handle HTTP requests.
To access web pages of any web application, you need a web server. The web server will handle all the http requests for the web application e.g IIS is a web server for ASP.NET web applications and Apache is a web server for PHP or Java web applications.
Node.js provides capabilities to create your own web server which will handle HTTP requests asynchronously. You can use IIS or Apache to run Node.js web application but it is recommended to use Node.js web server.
Create Node.js Web Server
Node.js makes it easy to create a simple web server that processes incoming requests asynchronously.
@sameralsayed
sameralsayed / NPM - Node Package Manager
Created June 16, 2022 20:22
Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application.
NPM - Node Package Manager
Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application. It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
It has now become a popular package manager for other open-source JavaScript frameworks like AngularJS, jQuery, Gulp, Bower etc.
Official website: https://www.npmjs.com
NPM is included with Node.js installation. After you install Node.js, verify NPM installation by writing the following command in terminal or command prompt.
C:\> npm -v
2.11.3
@sameralsayed
sameralsayed / Export Module in Node.js
Created June 16, 2022 20:22
Here, you will learn how to expose different types as a module using module.exports.
Export Module in Node.js
Here, you will learn how to expose different types as a module using module.exports.
The module.exports is a special object which is included in every JavaScript file in the Node.js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever you assign to module.exports will be exposed as a module.
Let's see how to expose different types as a module using module.exports.
Export Literals
As mentioned above, exports is an object. So it exposes whatever you assigned to it as a module. For example, if you assign a string literal then it will expose that string literal as a module.
@sameralsayed
sameralsayed / Node.js Local Module
Created June 16, 2022 20:21
Local modules are modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. You can also package it and distribute it via NPM
Node.js Local Module
Local modules are modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. You can also package it and distribute it via NPM, so that Node.js community can use it. For example, if you need to connect to MongoDB and fetch data then you can create a module for it, which can be reused in your application.
Writing Simple Module
Let's write simple logging module which logs the information, warning or error to the console.
In Node.js, module should be placed in a separate JavaScript file. So, create a Log.js file and write the following code in it.
Log.js Copy
var log = {