This content originally appeared on DEV Community and was authored by Sanya_Lazy
"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
- 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
- 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
- Now create a new file as
index.js
orserver.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}`);
});
- Now save and start server using this command
npm start
- Now the server is started at port.
How to use env
- Create new
.env
file in project folder. - And assign like this
PORT=5000
- Now this is accessed like this
const port = process.env.PORT || 3000;
How to use nodemon
- Install this using this command
npm i nodemon
- To use this change code in
package.json
file
"scripts": {
"start": "nodemon index.js"
},
- 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.
If you have doubt, check this GitHub Project link.
Happy Coding 😴 - Be Lazy
Contact DM - Twitter(X)
Contact Mail - sanya.san@myyahoo.com
This content originally appeared on DEV Community and was authored by Sanya_Lazy
Sanya_Lazy | Sciencx (2024-09-22T20:18:40+00:00) Building a Server with Node.js Express. Retrieved from https://www.scien.cx/2024/09/22/building-a-server-with-node-js-express/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.