TheBrahminCoder - How to create Backend server locally using express JS
Here are the steps on how to create a backend server locally:
- Install Node.js and npm.
- Create a new folder and open it in your favorite editor.
- Initialize npm for your project by running the following command:
Code snippet
npm init -y
- Install Express by running the following command:
Code snippet
npm install express
- Create a new file called
index.js
and paste the following code:
Code snippet
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
- Run the server by running the following command:
Code snippet
node index.js
The server will now be running on port 3000. You can access it by opening your browser and navigating to http://localhost:3000
.
Here is a breakdown of what the code does:
- The first line imports the
express
module. - The second line creates a new instance of the
express
app. - The third line defines a route that will be triggered when someone visits the root path of the server (
/
). - The fourth line sends a simple message back to the client.
- The fifth line starts the server on port 3000.
Once you have followed these steps, you will have successfully created a backend server locally. You can now start developing your application!
Here are some additional resources that you may find helpful:
- Express documentation: https://expressjs.com/
- Node.js documentation: https://nodejs.org/en/docs/
- npm documentation: https://docs.npmjs.com/
Comments
Post a Comment