Exploring Node js and Express in 30 days (Day 1)

Exploring Node js and Express in 30 days (Day 1)

**

Ist Day: Fundamentals of Node Js and Express

**

*Node: Introduction.
*

Node.js is an open-source, server-side JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It allows you to run JavaScript code on the server, enabling the development of scalable, high-performance web applications.

**Concepts of Modules in Node.
**Node.js uses a module-based architecture to organize code into reusable units. Modules encapsulate related functionality and can be shared between different parts of an application. CommonJS is the module system used in Node.js, where each file is treated as a separate module.

**HTTP Protocol:
**HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the World Wide Web. It is a protocol that defines how messages are formatted and transmitted between clients and servers.

After Installing Node js in your computer, You will need to create a server for your project using http module.

**Creating a Server with http.createServer:
**The http.createServer() method is used to create an HTTP server instance in Node.js. It takes a callback function as an argument, which is executed every time a request is received by the server.

`const http = require(‘http’)

const port = 3000;
const server = http.createserver((req, res)=>{
res.writeHead(200, {‘Content-Type’ : ‘text/plain’});
res.write(‘Hello World!’)
res.end()

})
`

server.listen(port, ()=>{
console.log(App is listening to ${port});
})

with the above code you have successfully created a server. Now go over to your favorite brower and access the server using this link http://localhost:3000/.
Note: The request and response parameters in the call back function is the method the server use to get information from the client and also sends back data to the client.

Request represent the client request in an object format.

Note: The job of a web server is to open and read files, but node as a web server is not limited to only opening and readiling files, it has so many use features, the reason for express frame work.

**Reading Files with FS module.
**The fs module in Node.js provides file system-related functionality, including reading and writing files. You can use the fs.readFile() or fs.readFileSync() methods to read the contents of a file asynchronously or synchronously, respectively.
Here is how to call the FS module.

const fs = require(‘fs’)
fs.readfile(‘filepath’, function(err)=>{
if(err) return err
})

With this module (‘fs’) you can create files with three (3) different methods.
Namely:
fs.append() – This method adds additional data to an existing data.
fs.append(‘file’, newdata, function()=>{
})

fs.open() – this method open a file, if the file does not exist it creates it, Note: has a ‘w’ flag that stands for write.

fs.open(‘path’, ‘w’, (err)=>{
if(err) return err
})

fs.writeFile() – this method creates a file, if the file exist, it re-writes it entirely, if it doesnt, it creates it.
fs.writeFile(‘index’, ‘Hello World’, function(err){
if(err)return err;
})

So many more, Node js can do, But, express simplifies Node js and makes it fast and scalable, lets look into Fundamentals of Express.

Express.js:

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It simplifies the process of creating servers, handling routes, and managing middleware.

Why Express.js?
Express.js is popular among developers for its simplicity, flexibility, and extensibility. It offers a rich ecosystem of middleware and plugins, making it easy to add functionality to your applications. Express.js abstracts away the complexities of raw Node.js HTTP server handling, providing a more streamlined development experience.

Modules in Express.js:
Just as Node, Express.js follows a modular architecture, allowing you to organize your application into separate modules or components. Common modules in Express.js include routing, middleware, templating engines, and database integration libraries.

Installation of Express:
Express.js can be installed via npm (Node Package Manager) using the command npm install express, npm was installed in your pc when you installed node js. This command installs the latest version of Express.js and adds it as a dependency to your project’s package.json file.

Installing Express Generator:
Express Generator is a command-line tool that generates a boilerplate Express.js application structure. It can be installed globally using npm with the command npm install -g express-generator.

When installing the template engine you have to specificy the engine you want, here is how you write your code, npx express-generator –view=ejs, this will install the ejs template engine.

Creating a Server in express is similar to node js. Here is how to create a server.

`const express = require (‘express’);
const app = express();

port = 3000;

app.get(‘/’, function(req, res){
res.sendFile(‘index.js’)
})

app.listen(port, function(){
console.log(App is listening to port ${port})
}).

Express js support some development tasks like, GET, POST, DELETE method which node js does not support.

Conclusion:
Node.js and Express.js are powerful tools for building server-side web applications in JavaScript. Understanding the core concepts and features of both platforms is essential for developing efficient and scalable web applications. Join me as i go deeper into express js frame work, cheers..

Leave a Reply

Your email address will not be published. Required fields are marked *