-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (50 loc) · 2 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
const path = require("path");
const express = require("express");
const app = express();
const { db, User, Image } = require("./server/db");
const PORT = process.env.PORT || 8000;
// const seed = require('./seed.js');
global.__basedir = __dirname;
require("dotenv").config();
//--------------------->Body parser <---------------------//
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
//--------------------->Loggin middleware morgan https://github.com/expressjs/morgan <---------------------//
const morgan = require("morgan");
app.use(morgan("dev"));
//--------------------->Serving javascript files, css files, and images from public folder<---------------------//
//https://expressjs.com/en/starter/static-files.html
// static file-serving middleware
app.use(express.static(path.join(__dirname, ".", "public")));
// app.use(express.static(path.join(__dirname, ".", "resources")));
app.use("/resources", express.static(path.join(__dirname, "resources")));
// API routes are prefixed with /api/ -
// this is purely done to namespace them away from your "front-end routes" (such as those created by react-router).
require("./server/routes/auth.route")(app);
require("./server/routes/user.route")(app);
require("./server/routes/image.route")(app);
// app.use("/resources", express.static(__dirname + "/resources"));
// app.use("/static", express.static(path.join(__dirname, "resources")));
app.get("*", function (req, res, next) {
res.sendFile(path.join(__dirname, "./public/index.html"));
});
app.use(function (err, req, res, next) {
console.error(err);
console.error(err.stack);
res.status(err.status || 500).send(err.message || "Internal server error.");
});
//--------------------->Starting server<--------------------- //
const startServer = () => {
const server = app.listen(process.env.PORT || PORT, () =>
console.log(`Listening on ${PORT}`)
);
};
(async () => {
await db.sync({});
console.log("db sync");
})(startServer());