express and node pakages
- Install Node.js:
Download and install Node.js from the official website: https://nodejs.org/
Create a new Express.js project:
- Open a terminal and navigate to the desired project directory.
- Run
npm init -y
to initialize a new Node.js project with default settings. Run
npm install express
to install the Express.js framework.Create an Express app:
- Create a new file (e.g.,
app.js
) in your project directory. - Require Express by adding
const express = require('express');
at the top of your file. Create an instance of the Express app:
const app = express();
Define routes:
- Define routes using
app.get()
,app.post()
, etc. methods on theapp
object. Example:
app.get('/', (req, res) => { res.send('Hello, World!'); });
Start the server:
- Add a listener to start the server on a specific port.
Example:
app.listen(3000, () => { console.log('Server is running on port 3000'); });
Run the Express app:
Execute
node app.js
in the terminal to run your Express application.Access the app in a web browser:
- Open a web browser and navigate to
http://localhost:3000/
(or the specified port) to see your Express app in action.