app.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const express = require('express');
  2. const path = require('path');
  3. const app = express();
  4. const { getLocale } = require('./libs/utils');
  5. const config = require('./config/app')
  6. // 设置视图引擎为EJS
  7. app.set('view engine', 'ejs');
  8. // 设置视图目录
  9. app.set('views', path.join(__dirname, 'views'));
  10. app.use(express.static(config.STATIC_DIR));
  11. app.use(express.static(path.join(__dirname, 'dist')));
  12. app.use('/thumbs/v1', require('./routes/res/thumbs'));
  13. app.use('/', require('./routes/index'));
  14. app.use('/proxy', require('./routes/proxy'));
  15. // catch 404 and forward to error handler
  16. app.use(function (req, res) {
  17. // 设置状态码为404
  18. res.status(404);
  19. // res.sendFile(path.join(__dirname, '404.html'));
  20. res.render('404', { title: '404 Error', description: 'PAGE NOT FOUND' });
  21. });
  22. // error handler
  23. app.use(function (err, req, res, next) {
  24. // set locals, only providing error in development
  25. res.locals.message = err.message;
  26. res.locals.error = req.app.get('env') === 'development' ? err : {};
  27. console.log("error:" + err);
  28. // render the error page
  29. res.status(err.status || 500);
  30. res.render('404', { title: '404 Error', description: 'PAGE NOT FOUND' });
  31. });
  32. // 启动服务器,监听3000端口
  33. const PORT = process.env.PORT || 3000;
  34. app.listen(PORT, () => {
  35. console.log(`Server is running on http://localhost:${PORT}`);
  36. });