Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

5 Intro to Backend Development With Nodejs

Uploaded by

Arlie Jay Morata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

5 Intro to Backend Development With Nodejs

Uploaded by

Arlie Jay Morata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5_Intro to Backend Development with nodejs.

md 2024-10-27

Session 5: Introduction to Backend Development with


Node.js
1. Introduction to Node.js
What is Node.js?

A JavaScript runtime built on Chrome's V8 engine.


Enables full-stack development by allowing JavaScript to be used for both frontend and backend.

Why Node.js?

Non-blocking, event-driven architecture: Efficiently handles I/O-heavy tasks.


Extensive ecosystem: npm provides over a million libraries for rapid development.

2. Setting up Node.js and npm


Steps:

1. Install Node.js from the official website.


2. Verify the installation:

node -v
npm -v

3. npm: Node Package Manager for managing project dependencies.

3. Building a Simple Server with Express.js


What is Express.js?

A minimal framework for building web apps and APIs with Node.js.

Create a Basic Server

1. Initialize a new project:

mkdir my-first-server
cd my-first-server
npm init -y

2. Install Express:

1/3
5_Intro to Backend Development with nodejs.md 2024-10-27

npm install express

3. Create a simple server in index.js:

const express = require("express");


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

app.get("/", (req, res) => {


res.send("Hello, World!");
});

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

4. Run the server:

node index.js

Visit http://localhost:3000 in a browser.

4. Hands-on Exercise: Creating a RESTful API with Node.js


Task: Build a Simple RESTful API

1. Define routes in index.js:

const express = require("express");


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

app.use(express.json());

let users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];

// GET all users


app.get("/users", (req, res) => {
res.json(users);
});

// GET a user by ID

2/3
5_Intro to Backend Development with nodejs.md 2024-10-27

app.get("/users/:id", (req, res) => {


const user = users.find((u) => u.id == req.params.id);
user ? res.json(user) : res.status(404).send("User not found");
});

// POST a new user


app.post("/users", (req, res) => {
const newUser = { id: users.length + 1, name: req.body.name };
users.push(newUser);
res.status(201).json(newUser);
});

// DELETE a user by ID
app.delete("/users/:id", (req, res) => {
users = users.filter((u) => u.id != req.params.id);
res.status(204).send();
});

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

2. Test the API:

Use tools like Postman to test GET, POST, and DELETE endpoints.

5. Discussion: The Role of Node.js in Modern Web Development


Why Use Node.js?

Scalability: Ideal for real-time apps like chat apps or games.


Efficiency: Handles many requests simultaneously due to its non-blocking model.

Common Use Cases

Real-time applications: Chat, multiplayer games.


Streaming services: Video streaming apps.
REST APIs: Often used in microservices architectures.

6. Homework Assignment
Extend your API to handle PUT requests for updating users.
Add error handling for when a user isn't found.

3/3

You might also like