-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
34 lines (30 loc) · 1.15 KB
/
index.mjs
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
import express from 'express';
import cookieParser from 'cookie-parser';
import methodOverride from 'method-override';
import cors from 'cors';
import bindRoutes from './routes.mjs';
const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:3000';
// Init Express instance
const app = express();
// Bind following app-level middleware
app.use(cors({
credentials: true, // enable HTTP cookies over CORS
origin: FRONTEND_URL, // restrict AJAX acess to single origin
}));
// Set Express view engine to expect EJS template
app.set('view engine', 'ejs');
// Parse cookie header and handle cookie separation & encoding
app.use(cookieParser());
// Parse incoming requests with JSON payloads
app.use(express.json());
// Parse request bodies for POST requests
app.use(express.urlencoded({ extended: false }));
// Override middleware to parse PUT and DELETE requests sent as POST requests
app.use(methodOverride('_method'));
// Serve static files stored in public folder
app.use(express.static('public'));
// Bind route definitions to Express app
bindRoutes(app);
// Start Express server and listen on given port
const PORT = process.env.PORT || 3004;
app.listen(PORT);