EXPRESS JS

EXPRESS JS

How To Create An Express Server
Express.js
Express.js, or simply Express, is a back end web application framework for building RESTful APIs with Node.js. It is designed for building web applications and APIs

Step 1: Download a Code Environment
Download any code environment of your choice e.g Vscode, Atom, WordPress, Sublime, Bracket e.t.c

Step 2: Download Node.js
Download Node.js on your browser. Click here to download.
alt text


Once the Node.js is installed it come s with a package called npm(Node Package Manager).

Step 3: Create a directory
Open your terminal: Use the terminal of your choices then run the following command line

// To create a folder;
mkdir Tech_Zone
// To switch to a folder
cd Tech_Zone
// To clear
clear

Step 4: Create a file_name
Create a file in the folder that has been created.

// To clear
clear
code . // Command to move directly to your vscode

Step 5: Create a package.json and install express
create a package using npm init -y, then install express

npm install (express) // Install express

Step 6: Install devDependies
DevDependencies are the packages a developer needs during development
Install the devDepdencies using npm install nodemon –save-dev
nodemon is a devDependencies used to run console on the terminal.

npm install nodemon savedev

Step 7: Create a server

// Import express to your file
const express = require(express);
// Call the express function
const server = express;

// What the users will get after running the server
server.get( /,(req, res) => {
res.send(Hello World);

// Create any port of your choices from 0 – 65535
const port = 5050;

server.listen(port, () => {
console.log(`Server is running on http://localhost${port}`);
})
})

Lastly: Run the server on the terminal
Run the server using npm start or npm run dev

npm start //or
npm run dev

Note: When using npm start you can only use the server once, this means you have to stop the server before restarting While using npm run dev it will restart itself and it make your work earsier

Leave a Reply

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