mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 04:11:26 +08:00
73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const morgan = require('morgan');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
|
|
// Middleware declarations.
|
|
|
|
// Add the logging middleware only if we aren't testing.
|
|
if (app.get('env') !== 'test') {
|
|
app.use(morgan('dev'));
|
|
}
|
|
|
|
app.use(bodyParser.json());
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'ejs');
|
|
|
|
// Routes.
|
|
app.use('/api/v1', require('./routes/api'));
|
|
app.use('/client', express.static(path.join(__dirname, 'dist')));
|
|
app.use('/admin', require('./routes/admin'));
|
|
|
|
//==============================================================================
|
|
// ERROR HANDLING
|
|
//==============================================================================
|
|
|
|
const ErrNotFound = new Error('Not Found');
|
|
ErrNotFound.status = 404;
|
|
|
|
// Catch 404 and forward to error handler.
|
|
app.use((req, res, next) => {
|
|
next(ErrNotFound);
|
|
});
|
|
|
|
// General error handler. Respond with the message and error if we have it while
|
|
// returning a status code that makes sense.
|
|
if (app.get('env') === 'development') {
|
|
app.use('/api', (err, req, res, next) => {
|
|
res.status(err.status || 500);
|
|
res.json({
|
|
message: err.message,
|
|
error: err
|
|
});
|
|
});
|
|
|
|
app.use('/', (err, req, res, next) => {
|
|
res.status(err.status || 500);
|
|
res.render('error', {
|
|
message: err.message,
|
|
error: err
|
|
});
|
|
});
|
|
}
|
|
|
|
app.use('/api', (err, req, res, next) => {
|
|
res.status(err.status || 500);
|
|
res.json({
|
|
message: err.message,
|
|
error: {}
|
|
});
|
|
});
|
|
|
|
app.use('/', (err, req, res, next) => {
|
|
res.status(err.status || 500);
|
|
res.render('error', {
|
|
message: err.message,
|
|
error: {}
|
|
});
|
|
});
|
|
|
|
module.exports = app;
|