-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (26 loc) · 819 Bytes
/
Copy pathserver.js
File metadata and controls
34 lines (26 loc) · 819 Bytes
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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const app = express();
// Middleware
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Routes
app.use('/', indexRouter);
// 404 Error handler
app.use((req, res, next) => {
res.status(404).json({ error: 'Not Found' });
});
// General Error Handler
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error stack for debugging
res.status(err.status || 500).json({ error: err.message || 'Internal Server Error' });
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
module.exports = app;