-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (39 loc) · 1.14 KB
/
server.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
import express from 'express';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import path, { dirname } from 'path';
// construct path
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import userRoutes from './routes/user.js';
// Load environment variables
dotenv.config();
const PORT = process.env.PORT || 5004;
// Initialize express
const app = express();
// parse body and cookies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// serve static files
app.use(express.static(path.join(__dirname, 'public')));
// use ejs template engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Use routes
app.use('/user', userRoutes);
// error
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ message: 'Internal Server Error' });
});
// handle 404
app.use('*', (req, res) => {
res.status(404).render('404', {
title: '404 Not Found',
message: 'Page not found'
});
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT}`);
});