Skip to content

Commit

Permalink
update and add pages
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCurrin authored Aug 29, 2024
1 parent cd1f6f8 commit 63f38e1
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 157 deletions.
130 changes: 3 additions & 127 deletions cheatsheets/javascript/node/web-servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,132 +83,8 @@ Result:
```


## Express
## 3rd-party packages

## Resources
See the [Express static server][] page for using Express.

- [expressjs.com](https://expressjs.com/) homepage.
- [express](https://www.npmjs.com/package/express) package on NPM.

See also project - [MichaelCurrin/express-js-rest-quickstart](https://github.com/MichaelCurrin/express-js-rest-quickstart). Including the notes in the docs.

### Install

Install Express.

```sh
$ npm install express
```

Note that `return` is not used in examples below - just `resp.send`.

### 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.
### 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);
});
```
### Static files
Serve a directory of static files.
- `index.js`
```javascript
const express = require('express');

const app = express();
const port = 3000;
const staticDir = 'build';

app.use('/', express.static(staticDir));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}/`));
```
[Express static server]: {% link cheatsheets/javascript/packages/express/static-server.md %}
67 changes: 67 additions & 0 deletions cheatsheets/javascript/packages/express/api.md
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);
});
```
30 changes: 30 additions & 0 deletions cheatsheets/javascript/packages/express/basic.md
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.
39 changes: 9 additions & 30 deletions cheatsheets/javascript/packages/express/index.md
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);
```
50 changes: 50 additions & 0 deletions cheatsheets/javascript/packages/express/static-server.md
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}/`));
```
4 changes: 4 additions & 0 deletions cheatsheets/javascript/packages/static-web-servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Or use `npx` to run without installing (useful if you want to try out a few and

You can also add the run commands to `package.json`. e.g. for `start` you can put `serve -p 8080`.

See the [Express static server][] page for using Express.

[Express static server]: {% link cheatsheets/javascript/packages/express/static-server.md %}

In alphabetical order:

- [glance](https://www.npmjs.org/package/glance)
Expand Down

0 comments on commit 63f38e1

Please sign in to comment.