Skip to content

Commit 25d2313

Browse files
Build a web API with Node.js and Express
1 parent f56d206 commit 25d2313

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Building a web API with Node.js and Express is a common task. Below is a simple example to get you started:
2+
3+
1. First, make sure you have Node.js and npm installed on your machine.
4+
5+
2. Create a new directory for your project and navigate to it in your terminal.
6+
7+
3. Initialize a new Node.js project by running:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Web servers and web applications
2+
3+
A web server is a piece of software that responds to requests from clients. A web application sits on top of the web server. Some environments, like Node.js, provide both the web server and the web application in a framework. In this module, the web server is provided by the HTTP module. The web application is provided by the Express.js framework and includes the web server.
4+
5+
Learn more:
6+
7+
- Web applications: The application delivers a web app to the client:
8+
Visually with HTML, CSS, and JavaScript
9+
Data with APIs
10+
Both visual and data with a combination of HTML, CSS, JavaScript, and APIs. This is considered a monolithic application.
11+
- URL Routing: URL routing is a mechanism to provide functionality of the web server when a specific URL address is requested. For example, the URL /products might be associated with a function that returns a list of products. The URL /products/1 might be associated with a function that returns a specific product.
12+
- HTTP Headers: These are key-value pairs that are sent from the client to the server. They contain information about the request or response.
13+
Support for different content types: A client can request data in a specific format and may return in that format such as plain text, JSON, HTML, or CSV.
14+
Authentication/Authorization: Some data might be sensitive. A user might need to sign in or have a specific role or permission level to access the data. This is handled in the HTTP header.
15+
- Data exchange: Users may need to view and add data to the system. To add data, users might enter data in a form or upload files.
16+
Time to market: To create web applications and APIs efficiently, choose tools and frameworks that provide solutions to common problems. These choices help the developer quickly meet the business requirements of the job.
17+
18+
## HTTP module in Node.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Include the HTTP module
2+
const http = require("http");
3+
4+
// Set the port to 3000
5+
const PORT = 3000;
6+
7+
// 1. Process incoming requests (req), reply with response (res)
8+
const requestHandler = (req, res) => {
9+
res.writeHead(200, { "Content-Type": "text/plain" });
10+
res.end("hello world");
11+
};
12+
13+
// 2. Create a server with the requestHandler
14+
const server = http.createServer(requestHandler);
15+
16+
// 3. Start listening for incoming requests on port
17+
server.listen(PORT, () => {
18+
console.log(`listening on port ${PORT}`);
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit e17b32216df5123cc679c6c8505aa96989805368

0 commit comments

Comments
 (0)