How to set up Node and Express js project.
Setting up a Node.js and Express.js project can be done in a few simple steps:
- Install Node.js on your machine: You can download the installer for your operating system from the official Node.js website at https://nodejs.org.
- Create a new directory for your project: Navigate to the directory where you want to create your project and run the following command to create a new directory:
mkdir my-express-app
3. Initialize a new Node.js project: Change into the newly created directory and run the following command to initialize a new Node.js project:
cd my-express-app
npm init -y
This will create a package.json
file that includes information about your project and its dependencies.
4. Install Express.js: Run the following command to install Express.js as a dependency for your project:
npm install express
5. Create an app.js
file: Create a new file named app.js
in your project directory and add the following code to it:
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');
});
This code sets up a basic Express.js server that listens on port 3000 and responds with “Hello, world!” when you navigate to http://localhost:3000 in your web browser.
6. Start the server: Run the following command to start the server:
node app.js
You should see the message “Server listening on port 3000” in your terminal. You can now navigate to http://localhost:3000 in your web browser to see the “Hello, world!” message.
Congratulations, you’ve set up a basic Node.js and Express.js project!