Local Web Server
Relevant source files
The following files were used as context for generating this wiki page:The Local Web Server is a small, self-contained Express.js application designed for local development. Its primary purpose is to provide a simple web interface for manually adding and viewing news posts. These manually entered posts are stored in a manual-posts.json file at the root of the project.
This server facilitates the creation of content that is not automatically gathered from other sources like GitHub issues or releases. The data saved by this server is later consumed by other scripts, such as scripts/collect-manual-posts.js, to be included in the final RSS feed and website build process.
Sources: server.js, add-post.html, scripts/collect-manual-posts.js
Architecture and Initialization
The server is built using the Express.js framework. It utilizes built-in Node.js modules like fs/promises for file system operations and path for handling file paths. The server is configured to parse JSON request bodies and serve static files from the project's root directory.
Sources: server.js:1-11, package.json:44
Server Startup
The server is initiated by running the start script defined in package.json. This command executes node server.js, which starts the Express application and makes it listen for incoming requests on a predefined port.
- Command:
npm run start - Port: 3000
- Output: Logs "Server running at http://localhost:3000" to the console upon successful startup.
Sources: package.json:8, server.js:52-54
The following diagram illustrates the server's startup process and how it handles incoming requests.
Sources: server.js:1-54, package.json:8
API Endpoints
The server exposes a simple REST API to manage manual posts, along with a root endpoint to serve the user interface.
Sources: server.js:13-49
| Method | Path | Description |
|---|---|---|
GET | / | Serves the main user interface file, add-post.html. This page contains a form to submit new posts and a list of existing manual posts. |
GET | /api/posts | Retrieves all posts from manual-posts.json. If the file does not exist, it returns an empty JSON array []. On other read errors, it returns a 500 status. |
POST | /api/posts | Adds a new post to manual-posts.json. It reads the existing file, appends the new post with a generated id and published_at timestamp, and writes the updated content back to the file. Returns the newly created post object with a 201 status on success. |
Sources: server.js:13-49, add-post.html
Data Structure
Posts are stored in manual-posts.json as a JSON array of objects. Each post object has the following structure:
| Field | Type | Description |
|---|---|---|
title | string | The title of the post. |
description | string | The main content/body of the post. |
link | string | A URL associated with the post. |
published_at | string | The publication date in ISO 8601 format. If not provided on creation, it defaults to the current time. |
id | number | A unique identifier generated using Date.now() at the time of creation. |
Sources: server.js:39-44, add-post.html:19-32
Frontend Interaction
The user interacts with the server via the add-post.html page. This page contains client-side JavaScript that communicates with the server's API to fetch and submit posts.
- Page Load: On page load, the
getPosts()function is called. It sends aGETrequest to/api/posts. - Display Posts: The server responds with the list of posts from
manual-posts.json. The client-side script then dynamically renders these posts on the page. - Add New Post: The user fills out the form and clicks "Add Post".
- Submit Form: A
submitevent listener on the form prevents the default submission, gathers the form data, and sends aPOSTrequest to/api/postswith the new post data in the request body. - Server Processing: The server receives the request, adds the new post to
manual-posts.json, and responds with a201 Createdstatus and the new post object. - Update UI: If the
POSTrequest is successful, the form is reset, andgetPosts()is called again to refresh the list of posts displayed on the page.
Sources: add-post.html:43-80, server.js:17-49
The sequence diagram below shows the flow of adding a new post.
Sources: server.js:13-49, add-post.html:43-80
Summary
The Local Web Server is a development utility that provides a straightforward way to add manual entries to the news feed. By running a simple Express.js server, it exposes a web form and a minimal API to manage posts stored in manual-posts.json. This functionality is essential for supplementing the automated content collection process, ensuring that any relevant news can be easily included in the project's final output.
Sources: server.js, package.json:8, scripts/collect-manual-posts.js