Things I Have Learned From My Very First Backend Approach
By: Mohd Yasir
Tags: Backend, JavaScript, Programming, Expressjs, Nodejs

Introduction

Starting from Node JS , Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment .

Creating Our Own Server

Now we will create a HTTP server listening on port 3000 , which sends Hello, World! to the browser. Note that, instead of using port 3000, you can use any port number you want , which is currently not in use by any other service!

HTTP

We are Going to use HTTP Module , so http module is a core module which comes with node itself , we dont need to install it . Now that http module gives us a functionality to create a server : “http.createServer()” .

Now lets code that and understand that :

const http = require('http'); http.createServer((request, response) => { response.writeHead(200, {

'Content-Type': 'text/plain'

}); response.write('Hello World!

'); response.end();

}).listen(3000);

The first line simply loaded the http module and then we are using createServer() method on http .

Now the third line of code says to the browser that status code 200 , means “OK” , and accepting the plain text .

Then we are writing hello world in our body .

Response.end will tell the browser that bodies and headers are passed now .

And in the last line we are staying browser to listen this on 3000 port .

Now save the file as helloworld.js and call in terminal as “node helloworld.js” . After that go to your browser and type “http://localhost:3000” .

Using Express will make your code shorter because its based on web application to avoid writing those plain text codes .

Some HTTP Status Codes

Status Code near 100 are Informational .

are Informational . Status code near 200 are Success , and 201 as Created and 204 as No Content .

are Success , and as Created and as No Content . Status code near 400 are Client Error , and 400 as Bad Request and 401 as Unauthorized and 404 as Not Found .

are Client Error , and as Bad Request and as Unauthorized and as Not Found . Status code near 300 as Redirection and 304 as Not Modified .

as Redirection and as Not Modified . Status code near 500 as Server Error .

Program Life Cycle & Event Loop

NodeJS runs non-blocking JavaScript code and uses an event-driven code or event loop for running your logic .

A Node program exits as soon as there is no more work to do .

But the createServer() event never finishes by default .

NodeJS & Core Modules

NodeJS ships with multiple core module (http,fs,path…) .

Core modules can be imported into any file to be used there .

Import via require (‘module’) .

NPM (Node Package Manager)

It allows you to manage node projects and its dependencies .

You can initialize a project with npm init .

NPM scripts can be defined in the package.json to give you shortcuts to common tasks .

3rd Party Package

Node projects typically dont just use core modules and custom code but also third-party packages .

You install them via npm .

You can differentiate between production dependencies ( — save) , development dependencies ( — save-dev) and global dependencies (-g) .

Types of Errors

Syntax , runtime and logical errors can break your app

Syntax and runtime errors throw (helpful) error messages (with line numbers!)

Logical errors can be fixed with testing and the help of the debugger .

What is ExpressJS ?

Express.JS is NodeJS frmework — a package that adds a bunch of utility functions and tolls and a clear set of rules on how the app should be built (middleware!) .

Its highly extensible and other packages can be plugged into it (middleware!) .

Middleware , next() and res()

ExpressJS relies heavily on middleware functions — you can easily add them by calling use() .

Middleware functions handle a request and should call next() to forward the request to the next function in line or send a response .

Routing

You can filter requests by path and method .

If you filter by method , paths are matched exactly , otherwise , the first segment of a URL is matched .

You can use the express.router to split your routes across files elegantly .

Serve Files

You are not limited to serving dummy text as a response

You can sendFile()s to your users , eg HTML files .

If a request is directly made for a file (eg a css file is requested) , you can enable static serving for such files via express.static()

Model View Controller (MVC)

Model

Responsible for representing your data .

Responsible for managing your data (saving , fetching )

View

What the user sees .

Should not contain too much logic .

Controller

Connects model and view .

Should only make sure that the two can communicate ( in both directions ) .

Connecting Our DB to Node Server