How to create a backend application server with Node Expressjs

Procedure:

·         We will create a git repository empty placeholder called backend2024

·         Then we will clone our placeholder inside our local work directory, in my case is C:\My Projects

·         After cloning we can check that the folder C:\My Projects\backend2024 was created

·         We move inside backend2024 folder

·         Check if node is installed by checking its version Check nodejs is installed or not by command: node -v

·         Now let’s start by installing Express.js applications server (backend), inside our local git folder backend2024 by following these instructions

Installing Express (expressjs.com)

Express “Hello World” example (expressjs.com)

·         Installation of nodedemon: https://www.npmjs.com/package/nodemon

Next: How to implement CRUD RestAPIs using Sequelize 

Procedure execution:

Git repository creation

In GitHub we will create git repository empty placeholder called backend2024

We click in the green Code button to copy the link that we need to clone the repository place holder in our local work directory folder

Clone repository in our local work directory

To be able to clone our private repository, we need to create a token pat, in this article of stack overflow, we have the instructions: https://stackoverflow.com/questions/2505096/clone-a-private-repository-github

https://github.com/settings/tokens/new

On my local machine I will go to my projects folders, open a terminal, and check if if git is installed, by executing this command: git version

In my case, I will clone inside this folder C:\My Projects

Now I will use the token pat from GitHub to clone the frontend2024 project, this is the command

git clone https://<pat>@github.com/<your account or organization>/<repo>.git

in this example case, will be:

git clone https://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@github.com/Ygabriele/backend2024.git

I will open the terminal in C:\My Projects and execute the clone command

If we run the command ls we can see that the local folder for the empty placeholder project “backend2024” was created.

Now backend2024 folder will be our local container of our backend app

Installing Express.js

Now let’s move into our local, app project folder running the command cd backend2024

Assuming you’ve already installed Node.js, use the npm init command to create a package.json file for your application.

This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:

entry point: (index.js)

Enter app.js, or server.js or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name. In our case let’s called app.js

Now install Express inside  backend2024 directory and save it in the dependencies list. For example:

npm install express

Create a Hello World Application Server

In my case I have indicated that our main start file will be called app.js

So, we need to create that file app.js and copy and paste this code inside the main program file app.js:

const express = require(‘express’)

const app = express()

const port = 3000

app.get( ‘/’, (req, res) => { res.send(‘Hello World!’) } )

app.listen(port, () => { console.log(`Example app listening on port ${port}`) })

To start Express.js application server using as main system file app.js, we need to execute this command:

Node app.js

Nodedemon installation

https://www.npmjs.com/package/nodemon

nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.

nodemon does not require any additional changes to your code or method of development.

To install use this command:

npm install -g nodemon

Usage

nodemon wraps your application, so you can pass all the arguments you would normally pass to your app:

nodemon [your node app]

In our case, now we will use

Nodemon app.js

Instead of

Node app.jos

Executing the app with nodemon, will allow the system to restart the server on the fly if a change occurs in our development files.          


Comments

Leave a Reply

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