forked from Pythagora-io/codebase-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (38 loc) · 1.44 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
require('dotenv').config();
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const routes = require('./routes');
console.log('Starting CodeWhisperer server...'); // Log for server start process
// Establish a connection to the database
require('./database');
// Middleware for parsing request bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Serve static content from 'public' directory
app.use(express.static('public'));
// Set EJS as the view engine
app.set('view engine', 'ejs');
console.log('EJS view engine set.');
// Set the directory for EJS templates
app.set('views', path.join(__dirname, 'views'));
console.log(`Views directory set to ${path.join(__dirname, 'views')}.`);
// Include our routes with the app
app.use('/', routes);
// Error handling middleware for not found errors
app.use((req, res, next) => {
const error = new Error('Not Found');
error.status = 404;
next(error);
});
// Error handling middleware for all other types of errors
app.use((error, req, res, next) => {
console.error(`Error encountered: ${error.stack}`); // Log the full error stack trace
res.status(error.status || 500).send(error.message || 'Something broke!');
});
// Start server listening
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server is running on port ${port}`); // Log server listening port
});