-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd1f6f8
commit 63f38e1
Showing
6 changed files
with
163 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# API | ||
|
||
|
||
## Handle query parameters | ||
|
||
Example based on [Getting started with NodeJS for frontend developers - Part 2](https://www.youtube.com/watch?v=HL7J3GT5v14). | ||
|
||
|
||
Add the following to base example, before `.listen`. | ||
|
||
Note use of `.query`. | ||
|
||
- `index.js` | ||
```javascript | ||
app.get('/foo', (req, resp) => { | ||
const { name } = req.query; | ||
resp.send(`Hello, ${name || world}!`); | ||
}) | ||
``` | ||
|
||
Test on: | ||
|
||
- [localhost:3000/foo?name=Bar](http://localhost:3000/foo?name=Bar) | ||
|
||
|
||
## Handle URL parameters | ||
|
||
Add the following to base example, before `.listen`. | ||
|
||
Note use of `.params`. | ||
|
||
```javascript | ||
app.get('/foo/:name', (req, resp) => { | ||
const { name } = req.params; | ||
resp.send(`Hello, ${name || world}!`); | ||
}) | ||
``` | ||
|
||
Test on: | ||
|
||
- [localhost:3000/foo/Bar](http://localhost:3000/foo/Bar) | ||
|
||
|
||
## Handle POST request | ||
|
||
- `index.js` | ||
```javascript | ||
app.use(express.json()); | ||
app.use(express.urlencoded()); | ||
app.post('/foo', (req, res) => { | ||
res.status(201) | ||
.send({ | ||
message: "Created a foo using data", | ||
data: req.body, | ||
}); | ||
}); | ||
``` | ||
|
||
## Handle other status codes | ||
|
||
- `index.js` | ||
```javascript | ||
app.get('/admin', (_req, res) => { | ||
res.sendStatus(401); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Basic | ||
|
||
## Basic greeting | ||
|
||
[Express starter](https://expressjs.com/en/starter/hello-world.html) | ||
|
||
- `server.js` | ||
```javascript | ||
const express = require('express'); | ||
|
||
const app = express(); | ||
const port = 3000; | ||
|
||
app.get('/', (req, resp) => resp.send('Hello, world!')) | ||
|
||
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}/`)) | ||
``` | ||
|
||
Run as: | ||
|
||
```sh | ||
$ node server.js | ||
Example app listening at http://localhost:3000/ | ||
``` | ||
|
||
Open browser at: | ||
|
||
- [localhost:3000/](http://localhost:3000/) | ||
|
||
Note that you'll get an error on other paths. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,20 @@ | ||
# Express | ||
|
||
Express is a Node web application framwork, which can be used for serving static assets, building APIs and integratng with middlewares. | ||
|
||
## Static directory | ||
### Resources | ||
|
||
From [Static files](https://expressjs.com/en/starter/static-files.html) in the docs. | ||
- [expressjs.com](https://expressjs.com/) homepage. | ||
- [express](https://www.npmjs.com/package/express) package on NPM. | ||
|
||
Serve a static directory. e.g. serve the `public` directory. | ||
See also project - [MichaelCurrin/express-js-rest-quickstart](https://github.com/MichaelCurrin/express-js-rest-quickstart). Including the notes in the docs. | ||
|
||
```javascript | ||
app.use(express.static('public')) | ||
``` | ||
|
||
To serve on another path e.g. server contents of `public` as a `/static`. | ||
|
||
```javascript | ||
app.use('/static', express.static('public')) | ||
``` | ||
## Install | ||
|
||
Using an absolute path is safer. | ||
Install Express. | ||
|
||
```javascript | ||
const path = require('path') | ||
|
||
app.use('/static', express.static(path.join(__dirname, 'public'))) | ||
```sh | ||
$ npm install express | ||
``` | ||
|
||
A fuller example: | ||
|
||
```javascript | ||
import express from "express"; | ||
|
||
const PORT = 3000 | ||
|
||
const app = express(); | ||
|
||
app.use(express.static('public')); | ||
app.use(express.static('images')); | ||
|
||
app.listen(PORT); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
description: Configure Express to serve static assets | ||
--- | ||
# Static server | ||
|
||
From [Static files](https://expressjs.com/en/starter/static-files.html) in the docs. | ||
|
||
|
||
Serve a static directory. e.g. serve the `public` directory. | ||
|
||
```javascript | ||
app.use(express.static('public')) | ||
``` | ||
|
||
To serve on another path e.g. server contents of `public` as a `/static`. | ||
|
||
```javascript | ||
app.use('/static', express.static('public')) | ||
``` | ||
|
||
Using an absolute path is safer: | ||
|
||
```javascript | ||
const path = require('path') | ||
|
||
app.use('/static', express.static(path.join(__dirname, 'public'))) | ||
``` | ||
|
||
A fuller example: | ||
|
||
- `index.js` | ||
|
||
```javascript | ||
import express from "express"; | ||
|
||
const PORT = 3000 | ||
|
||
const app = express(); | ||
|
||
app.use(express.static('public')); | ||
app.use(express.static('images')); | ||
|
||
app.listen(PORT); | ||
``` | ||
|
||
Or | ||
|
||
```javascript | ||
app.listen(PORT, () => console.log(`Example app listening at http://localhost:${port}/`)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters