-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
77 lines (63 loc) · 2.13 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import serverless from 'serverless-http';
import express from 'express';
import bodyParser from 'body-parser';
import { create, remove, update, list, get } from './libs/service.lib';
const app = express();
app.use(bodyParser.json());
app.get('/', (_, res) => {
res.send(`
<style> body { font-family: sans; padding: 10vh; } </style>
<h1>Notes API - Serverless</h1>
<p>
This particular API Endpoint is running on express. <br />
I created new endpoints for express Notes API.
</p>
<p>Regular endpoints - serverless(fn):</p>
<ul>
<li><b>GET</b> /notes</li>
<li><b>POST</b> /notes</li>
<li><b>GET</b> /notes/:id</li>
<li><b>PUT</b> /notes/:id</li>
<li><b>DELETE</b> /notes/:id</li>
</ul>
<p>Express endpoint serverless(express);</p>
<ul>
<li><b>GET</b> /express/notes</li>
<li><b>POST</b> /express/notes</li>
<li><b>GET</b> /express/notes/:id</li>
<li><b>PUT</b> /express/notes/:id</li>
<li><b>DELETE</b> /express/notes/:id</li>
</ul>
`);
});
app.get('/express/notes/:id', async (req, res) => {
const response = await get(req.params.id, req.requestContext);
res.status(response.statusCode);
res.header(response.headers);
res.send(response.body);
});
app.get('/express/notes', async (req, res) => {
const response = await list(req.requestContext);
res.status(response.statusCode);
res.header(response.headers);
res.send(response.body);
});
app.put('/express/notes/:id', async (req, res) => {
const response = await update(req.params.id, req.body, req.requestContext);
res.status(response.statusCode);
res.header(response.headers);
res.send(response.body);
});
app.delete('/express/notes/:id', async (req, res) => {
const response = await remove(req.params.id, req.requestContext);
res.status(response.statusCode);
res.header(response.headers);
res.send(response.body);
});
app.post('/express/notes', async (req, res) => {
const response = await create(req.body, req.requestContext);
res.status(response.statusCode);
res.header(response.headers);
res.send(response.body);
});
export const main = serverless(app);