Express.js(TypeScript)のプロジェクトでrouterがうまく動かなくて困っています。
express-generatorで作成した、Express.jsプロジェクトをTypeScriptに変換して、
./src/index.tsを作成し、
ts-nodeで実行できるところまで確認しています。
この2つはjsonが返ってきますが、
http://localhost:3000
http://localhost:3000/api
./src/api/employeeApiController.tsを用意し、このControllerにアクセスさせようとして、
アドレスを入力してもアクセスできなくフリーズしています。
http://localhost:3000/api/employees
わかるかたいらっしゃればお願いします。
■./src/index.ts
TypeScript
1import express, { Request, Response, NextFunction } from "express"; 2import createError from "http-errors"; 3import path from "path"; 4import cookieParser from "cookie-parser"; 5import logger from "morgan"; 6 7const app = express(); 8 9app.use(logger("dev")); 10app.use(express.json()); 11app.use(express.urlencoded({ extended: false })); 12app.use(cookieParser()); 13 14// ルーターのマウント 15import { employeeApiController } from "./api/employeeApiController"; 16app.use("/api/employees", employeeApiController); 17 18// APIアクセス用のルートハンドラ 19app.get("/api", (req: Request, res: Response, next: NextFunction) => { 20 res.json({ message: "200 OK" }); 21}); 22 23// catch 404 and forward to error handler 24app.use(function (req: Request, res: Response, next: NextFunction) { 25 next(createError(404)); 26}); 27 28// error handler 29app.use(function (err: Error, req: Request, res: Response, next: NextFunction) { 30 // set locals, only providing error in development 31 res.locals.message = err.message; 32 res.locals.error = req.app.get("env") === "development" ? err : {}; 33 34 // render the error page 35 const httpStatusCode = (err as any).status || 500; 36 res.status(httpStatusCode).json({ message: httpStatusCode + " " + err.message }); 37}); 38 39// ポート指定 40const port = process.env.PORT || 3000; 41app.listen(port, () => { 42 console.log(`Server is running on port ${port}`); 43}); 44 45module.exports = app;
■./src/api/employeeApiController.ts
TypeScript
1import express, { Request, Response, NextFunction } from "express"; 2 3export const employeeApiController = () => { 4 const router = express.Router(); 5 6 // GET /api/employees 7 router.get( 8 "/", 9 (request: Request, response: Response, next: NextFunction) => { 10 async () => { 11 response.json({ message: "done" }); 12 }; 13 } 14 ); 15 16 return router; 17};
■ tsconfig.json
json
1{ 2 "compilerOptions": { 3 "target": "es6", 4 "module": "commonjs", 5 "outDir": "./dist", 6 "strict": true, 7 "esModuleInterop": true, 8 "sourceMap": true 9 }, 10 "include": ["./src/**/*.ts"], 11 "exclude": ["node_modules"] 12}
■ package.json
json
1{ 2 "name": "backend", 3 "version": "0.0.0", 4 "private": true, 5 "scripts": { 6 "dev": "ts-node ./src/index.ts", 7 "start": "node ./bin/www" 8 }, 9 "dependencies": { 10 "cookie-parser": "~1.4.4", 11 "debug": "~2.6.9", 12 "express": "~4.16.1", 13 "morgan": "~1.9.1" 14 }, 15 "devDependencies": { 16 "@types/cookie-parser": "^1.4.7", 17 "@types/debug": "^4.1.12", 18 "@types/express": "^4.17.21", 19 "@types/http-errors": "^2.0.4", 20 "@types/morgan": "^1.9.9", 21 "@types/node": "^16.11.10", 22 "ts-node": "10.9.1", 23 "ts-node-dev": "^2.0.0", 24 "typescript": "4.5.2" 25 } 26} 27
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/03/12 23:18
2024/03/13 11:23 編集
2024/03/13 11:48 編集