[[Node.js化]] ** [[基本的な流れ]] [#y69caab5] import express from 'express'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { port, routePrefix, basePath } from './config.js'; const app = express(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // View Engine Setup app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, 'views')); // 静的ファイルのマウント app.use('/static', express.static(path.join(__dirname, 'public'))); app.use(basePath, express.static(path.join(__dirname, 'public'))); app.use('/media', express.static(path.join(__dirname, 'public/media'))); // 動的ルートの自動登録 const routeDir = path.join(__dirname, 'routes'); fs.readdirSync(routeDir).forEach(file => { if (file.endsWith('.js')) { import(`./routes/${file}`).then(module => { app.use('/' + file.replace('.js', ''), module.default); }); } }); app.get('/', (req, res) => { res.render('site'); // views/site.ejs を表示 }); const port = process.env.PORT || 8080; app.listen(port, () => { console.log(`Listening at http://localhost:${port}`); console.log(`Tennis app listening at http://localhost:${port}${basePath}`); }); ** [[views(テンプレート)を使うための Express 側設定]] [#y69caab5] ** [[ルーティング(routes)でベースパスを使うには]] [#c7abe387] ** [[静的ファイルへのアクセス]] [#jdcae473] ** [[静的ファイルの読み込み]] [#t6c5d720]