-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (38 loc) · 1.11 KB
/
index.js
File metadata and controls
48 lines (38 loc) · 1.11 KB
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
// @ts-check
/**
* Simple REST Api used to teach Flutter.
*
* For endpoints / setup see the Readme
* License: MIT
*/
const express = require("express");
const rateLimit = require("express-rate-limit");
const morgan = require('morgan');
require("dotenv").config();
const handlers = require("./handlers");
const PORT = process.env.PORT || 5000;
const app = express();
// Limit each IP to 50 requests / 5 secs
const limiter = rateLimit({
windowMs: 5 * 1000,
max: 50,
});
// Middlewares:
app.use(limiter);
app.use(morgan('short'));
app.use(express.json());
// Endpoints:
app.get("/", (req, res) => {
res.status(418).json({
what_is_this: "Simple REST API for a flutter course",
readme: "https://github.com/davidp-ro/curs-flutter-api",
});
});
app.get("/sneakers", (req, res) => handlers.getAllSneakers(req, res));
app.get("/sneaker", (req, res) => handlers.getSneakerWithID(req, res));
app.get("/sneaker/:id", (req, res) => handlers.getSneakerWithID(req, res));
app.post("/sneaker", (req, res) => handlers.addSneaker(req, res));
// Start:
app.listen(PORT, () => {
console.log(`Running on port ${PORT}`);
});