-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.73 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
// if for automatic deployement purpose.
if (process.env.NODE_MODE !== "production") {
/* eslint-disable global-require */
require("dotenv").config({ path: `${__dirname}/../.env` });
/* eslint-enable global-require */
}
const express = require("express");
const cors = require("cors");
const { routes, db } = require("./controllers/Route");
const app = express();
const port = process.env.PORT || 3000;
// use cors for front end request compatibility
app.use(cors());
/* *******************************************************
* Express route to ask for a user
******************************************************* */
app.get("/user/:username", routes);
/* *******************************************************
* Default route
******************************************************* */
app.get("/", routes);
/* *******************************************************
* Error when route not foud
******************************************************* */
app.use((req, res) => {
console.log("Route not found");
res.status(404);
res.send("404 Route not found");
});
/* ******************************************************
* Error Handler
******************************************************* */
// Difficult to test
/* istanbul ignore next */
app.use((err, req, res) => {
console.log("Route not found");
res.status(err.status || 500);
res.send(err.message);
});
/* *******************************************************
* app start
******************************************************* */
// Test in index.test.js
/* istanbul ignore if */
if (process.env.NODE_MODE !== "test") {
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`);
});
}
// Use for testing
module.exports = { app, db, port };