DEV Community

Cover image for Building a Server with Node.js Express
Sanya_Lazy
Sanya_Lazy

Posted on

Building a Server with Node.js Express

"Welcome to the world of server-side development..." grabs attention and sets the tone.

What is Node.js ?

Node.js is a JavaScript runtime environment built on Chrome's V8 JavaScript engine. It allows you to run JavaScript on the server, making it possible to build dynamic web applications.

What is Express.js ?

Express.js, or simply Express, is a back-end web application framework that works on top of Node.js. It provides a robust set of features for building RESTful APIs and web applications. Express is designed to simplify the process of building web applications and APIs by providing a minimal and flexible framework.

Setting up Node.js with Express:

  • First Make sure you have Installed Node. If not refer to this link.
  • You can check if Node.js is properly installed by opening a terminal or command prompt and typing node -v. If Node.js is installed, it will display the version number.
  • And also check for npm version with this command:
npm -v  # to check npm version

node -v # to check node version
Enter fullscreen mode Exit fullscreen mode
  • Now Create a new folder πŸ“‚, and navigate and open terminal.
  • Now Initialize npm
npm init # Intialize
# It asks for yes or no, type y. And also it asks some questions, you can skip or give.

# Otherwise directly you can use this
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Now after initialized you get a package.json file. This is project configuration and also you can check npm installed packages.
  • These are some packages you have to install
npm install express
# Installs the Express.js framework, which is a popular choice for building web applications with Node.js
npm install cors
# Installs the cors package, which helps you handle Cross-Origin Resource Sharing (CORS) requests in your Express.js application.
npm install body-parser
# Installs the body-parser middleware, which is used to parse incoming request bodies (like JSON data) in your Express.js application.
npm install dotenv
# Installs the dotenv package, which allows you to load environment variables from a .env file, keeping sensitive information separate from your code.
npm install nodemon
# Installs nodemon, a handy tool that automatically restarts your Node.js server whenever you save changes to your code. This makes development much smoother.

# Are else you can download all at a time
npm install express cors body-parser dotenv nodemon
Enter fullscreen mode Exit fullscreen mode
  • Now create a new file as index.js or server.js. Or else you can write any thing.
  • Now navigate to index.js
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
require("dotenv").config();

const app = express();
const port = process.env.PORT || 3000;

// Middleware
app.use(cors());
app.use(bodyParser.json());

// Routes
app.get("/", (req, res) => {
Β  res.send("Hello World!");
});

// Start the server
app.listen(port, () => {
Β  console.log(`Server is running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode
  • Now save and start server using this command
npm start
Enter fullscreen mode Exit fullscreen mode
  • Now the server is started at port.

How to use env

  • Create new .env file in project folder.
  • And assign like this
PORT=5000
Enter fullscreen mode Exit fullscreen mode
  • Now this is accessed like this
const port = process.env.PORT || 3000;
Enter fullscreen mode Exit fullscreen mode

How to use nodemon

  • Install this using this command
npm i nodemon
Enter fullscreen mode Exit fullscreen mode
  • To use this change code in package.json file
  "scripts": {
    "start": "nodemon index.js"
  },
Enter fullscreen mode Exit fullscreen mode
  • Now when you start server using npm start.
  • The server will start at port 5000.
  • Now if you make changes also the server do not need to restart.
  • Changes are implemented dynamically, eliminating the need for a server restart.

Testing:

  • Now to test this server. After starting server navigate to URL http://localhost:5000.
  • This will show Hello World!
  • You can also test this API in API development platform.

Express js
*Reference: *

postman UI

If you have doubt, check this GitHub Project link.

Happy Coding 😴 - Be Lazy

Contact DM - Twitter(X)
Contact Mail - sanya.san@myyahoo.com

Top comments (0)