DEV Community

Sanni Ridwan
Sanni Ridwan

Posted on

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

Image description
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
Enter fullscreen mode Exit fullscreen mode
// To switch to a folder
cd Tech_Zone
Enter fullscreen mode Exit fullscreen mode
// To clear
clear
Enter fullscreen mode Exit fullscreen mode

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

// To clear
clear
Enter fullscreen mode Exit fullscreen mode
code . // Command to move directly to your vscode
Enter fullscreen mode Exit fullscreen mode

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

Image description

npm install ("express") // Install express
Enter fullscreen mode Exit fullscreen mode

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 --save-dev
Enter fullscreen mode Exit fullscreen mode

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}`);
  })
})
Enter fullscreen mode Exit fullscreen mode

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

npm start //or 
npm run dev
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)