This repository was archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
68 lines (49 loc) · 1.69 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
62
63
64
65
66
67
68
// Import 3rd party packages/libs
import express from 'express'
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
import njk from 'nunjucks'
import mongoose from 'mongoose'
import dotenv from 'dotenv'
// Import routes for API and the actual site.
import siteRoutes from './routes/siteEntrypoint.js'
import apiRoutes from './api/apiEntrypoint.js'
// Load environment veriables from the '.env' file.
dotenv.config()
// Create an app being an instance of express - the library that handles the backend server.
const App = express()
// Connect to the database and shit.
mongoose.connect(
process.env.DB_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true
},
() => console.log("Database connected.")
)
// Configure middlewares for express. (Basically addons/plugins; extra functionality.)
App.use(bodyParser.urlencoded({ extended: true }))
App.use(bodyParser.json())
App.use(cookieParser())
// Setup nunjucks. Nunjucks uses the 'views' directory.
// Nunjucks is a rendering engine that allows you to reuse HTML code. (And more.)
njk.configure(
'views',
{
autoescape: false,
express: App
}
)
// Setup the static file directory 'static'. It contains non-changing assets and so on.
App.use(express.static('static'))
// Setup the server to use the imported routes on said paths.
App.use('/', siteRoutes)
App.use('/api', apiRoutes) // In the longterm you should consider binding them to versions like: /api/v1, /api/v2, etc.
// Start the server on port 5500
App.listen(5500, () => {
console.log("The server has started and is running on http://localhost:5500")
})
// CLEANUP
process.on('exit', () => {
mongoose.disconnect()
})