0% found this document useful (0 votes)
15 views4 pages

Pract 9

This document provides a step-by-step guide to setting up a Node.js project with Express. It covers installation of Node.js and npm, creating a project folder, initializing the project, installing Express, creating necessary files, writing a static HTML file, coding the server, and running the server. Each step includes explanations and commands for both macOS and Windows users.

Uploaded by

dipeshnawani16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Pract 9

This document provides a step-by-step guide to setting up a Node.js project with Express. It covers installation of Node.js and npm, creating a project folder, initializing the project, installing Express, creating necessary files, writing a static HTML file, coding the server, and running the server. Each step includes explanations and commands for both macOS and Windows users.

Uploaded by

dipeshnawani16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

🪛 Step 1: Install Node.

js and npm

✅ What is it?

Node.js lets us run JavaScript on the server.

npm (Node Package Manager) helps manage libraries like Express.

How to check:

Open your terminal or command prompt and run:

node -v
npm -v

If both versions show, you're good. If not, download and install from:
👉 https://nodejs.org/

---

📂 Step 2: Create Project Folder

mkdir express-pract9
cd express-pract9

✅ Why?

mkdir = make directory

cd = change into that directory

---

📦 Step 3: Initialize the Project

npm init -y

✅ Why?

This creates a package.json file, which keeps track of your project settings and dependencies.

-y skips the questions and sets defaults.


---

🔌 Step 4: Install Express

npm install express

✅ What is it?

Express is a web framework that makes creating servers easy.

📁 After this step, node_modules/ and package-lock.json will be created.

---

🧠 Step 5: Create Files and Folders

mkdir public
touch public/index.html
touch server.js

> On Windows, use:

mkdir public
echo > public\index.html
echo > server.js

✅ Why?

public/ will hold static files (HTML, CSS, images).

index.html is the main static file.

server.js is where you’ll write your Express code.

---

📄 Step 6: Write Static HTML File


Open public/index.html and add:

<!DOCTYPE html>
<html>
<head>
<title>Static Page</title>
</head>
<body>
<h1>Welcome to the Static Page</h1>
</body>
</html>

---

🔥 Step 7: Code the Server (server.js)

Paste this code:

const express = require('express');


const app = express();
const port = 3000;

// Serve static files from "public" folder


app.use(express.static('public'));

// Serve dynamic content at /hello route


app.get('/hello', (req, res) => {
res.send('<h1>Hello from Express!</h1><p>This is a dynamic HTML response.</p>');
});

// Start server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});

🧠 Explaining the Code:

require('express'): Import Express library.

express(): Create an Express app.

express.static('public'): Serve files in the public folder.

app.get('/hello'): Handle a GET request to /hello by sending back HTML.


app.listen(): Start the server on port 3000.

---

🚀 Step 8: Run the Server

node server.js

Now open your browser:

Static: 👉 http://localhost:3000/

Dynamic: 👉 http://localhost:3000/hello

You might also like