-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
50 lines (40 loc) · 997 Bytes
/
index.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
const express = require("express");
const { query, validationResult, body } = require("express-validator");
const PORT = process.env.PORT || 3000;
const app = express();
// middleware
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// view engine
app.set("view engine", "ejs");
// routes
app.get("/", (req, res) => {
res.render("home");
});
app.get("/scene", (req, res) => {
res.render("scene");
});
app.post(
"/scene",
body("url")
.notEmpty()
.isURL({ protocols: ["http", "https"] }),
(req, res) => {
console.log(req.body);
const result = validationResult(req);
if (result.isEmpty()) {
return res.render("scene", req.body);
}
return res.send({
msg: "req.body.url is not a proper URL",
errors: result.array(),
});
}
);
app.use(function (req, res) {
res.status(404).render("error404");
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});