Are you learning web development and confused about how to start a Node.js project? Don’t worry, I’ve got you! I’ll guide you through creating your first backend with Node.js and Express.js in just 5 steps.
1. Install Node.js and npm: Download and install Node.js from the official website. It comes with npm (Node Package Manager), which helps you manage packages.
2. Create a Project Folder: Make a folder for your project. Open the terminal (or command prompt) and type:
mkdir my-node-project cd my-node-project
3. Initialize Your Project: Inside the folder, set up a new Node.js project by typing:
npm init
This will create a package.json file where all your project info and dependencies are stored. Just press Enter for each question if you’re unsure.
4. Install Express.js: Express.js is a framework that makes building a backend easier. Install it by typing:
npm install express
Keeping things organized is important! Here’s how you can structure your project:
Example folder structure:
my-node-project/ ├── routes/ ├── server.js ├── package.json └── node_modules/
1. Create the File: Inside your project folder, create a file named server.js. This will be the entry point of your app.
2. Write Your First Node.js Server:
const express = require('express'); // Importing express const app = express(); // Creating an express app // Create a route that sends a response when visiting the homepage app.get('/', (req, res) => { res.send('Hello, Express.js Server!
'); }); // Set up the server to listen on port 3000 const port = 3000; app.listen(port, () => { console.log(`Server is running on port ${port}`); });
3. Test It: Run your server by typing:
node server.js
Now, open your web browser and go to http://localhost:3000. You should see “Hello, Express.js Server!” on the screen!
Routes define how your server responds to different URL requests.
1. Create a Route: In the server.js file, you already have one route:
app.get('/', (req, res) => { res.send('Hello, Express.js Server!'); });
2. Add More Routes: Let’s add some more routes:
app.get('/about', (req, res) => { res.send('This is the about page'); }); app.get('/contact', (req, res) => { res.send('This is the contact page'); });
3. Test Your Routes: After saving, go to your browser and visit these URLs:
To keep your server running and test changes:
1. Start Your Server: Run your server again:
node server.js
2. Test it: You can visit the URLs in your browser or use a tool like Postman to send requests.
3. Keep Your Server Updated: You can install nodemon, which automatically restarts the server when you change the code:
npm install -g nodemon
Now, instead of node server.js, run:
nodemon server.js
When you visit http://localhost:3000/, you'll see:
Hello, Express.js Server!
When you visit http://localhost:3000/about, you'll see:
This is the about page
Use this Printable Backend Developer Notion Template to Track Your Progress!
Beginner developers often struggle with choosing the right tech stack which lead them to wasted time and motivation loss. Thus, I created a beautifully design, very easy to follow 6-month backend developer roadmap in Notion so that you can track your progress and stick with your goals._
This roadmap:
Thanks for reading this article. Make sure to follow me on ? for the latest updates.
Read more: skills to become a backend developer in 6 months (roadmap)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3