Expanded guides for some APIs and Microservices challenges and fixed links to the source files (#36131)

* Expanded the solution for the 'Get Route Parameter Input from the Client' challenge

* Expanded the guide for the 'Use body-parser to Parse POST Requests' challenge

* Rewritten guide for the 'Serve JSON on a Specific Route' challenge and fixed source link

* Expanded the guide for the 'Serve Static Assets' challenge

* Expanded solution to the 'Get Query Parameter Input from the Client' challenge and fixed links to source file

* Added solution to the 'Chain Middleware to Create a Time Server' challenge and fixed link to source file

* Rewrite the 'Start a Working Express Server' challenge

* Expanded the guide for 'Expand Your Project with External Packages from npm'

* Added reference to semantic versioning in 'Add a Version to Your package.json'

* fix/remove-links+fix-solutions

* fix/remove-more-links
This commit is contained in:
Sudipto Ghosh
2019-07-02 07:05:15 +05:30
committed by Tom
parent 2e0efd7b94
commit 29823fe495
12 changed files with 122 additions and 84 deletions

View File

@@ -5,19 +5,18 @@ title: Serve Static Assets
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
Static webpages are fairly simple with express. This could be useful for building your own portfolio website or blog, etc.
Serving static webpages and assets is fairly simple with `express`. This could be useful for building your own portfolio website or blog, single-page web applications etc.
To serve a static webpage from the "views" folder you can use code such as:
To serve static assets from the `public` folder in the you can use the `express.static()` method as the middleware. This method takes the endpoint and the absolute path to the directory containing the static assets as arguments and exposes the files in that folder at the given endpoint. By default, if the endpoint is not passed to the method, the folder is exposed at the root endpoint i.e. `/` for the application.
The `__dirname` variable is a string containing the absolute path to the root of your project which has to be concatenated with the folder containing the assets.
Add the following line to your file above all defined routes to achieve the desired result:
```javascript
const express = require("express");
const app = express();
app.use(express.static(__dirname + "/views"));
// Normal usage
app.use(express.static(__dirname + "/public"));
// Assets at the /assets route
app.use("/assets", express.static(__dirname + "/public"));
```
<a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/apis-and-microservices/basic-node-and-express/meet-the-node-console/index.md' target='_blank' rel='nofollow'>Help our community expand these hints and guides</a>.