server.js 962 B

12345678910111213141516171819202122232425262728
  1. //server.js 專案起始點
  2. const express = require("express");
  3. const cookieSession = require("cookie-session");
  4. const dotenv = require('dotenv').config();
  5. const db = require('./pass_db.js');
  6. db.users_init_fixed()
  7. db.vote_init_random(5); // you COULD change this, but you could also leave it alone?
  8. const app = express();
  9. const PORT = process.env.PORT || 8000; // 允許從 .env 文件或環境變量中設定端口
  10. app.use(express.static("static"))
  11. app.set('view engine', 'ejs')
  12. app.use(cookieSession({
  13. name: 'session',
  14. keys: [process.env.SESSION_KEY || 'default_session_key'],
  15. maxAge: 24 * 60 * 60 * 1000 // 24 hours
  16. }))
  17. // 使用 Express 內建的中間件來解析 JSON 和 URL 編碼的資料
  18. app.use(express.json());
  19. app.use(express.urlencoded({ extended: true }));
  20. const apiRouter = require("./routers/api")
  21. app.use('/api/v1', apiRouter);
  22. app.listen(PORT, () => console.log(`server should be running at http://localhost:${PORT}/`))