0

当我运行时npm run dev,我无法在 chrome 中打开“http://localhost:3000”。

执行任务:

> npm run dev 

node-framework-kit@1.0.0 dev /Users/xiaoqi/node-workplace/node-framework-kit  
ts-node-dev --respawn --transpile-only ./src/app.ts

[INFO] 16:44:02 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.2)

截屏

这是我的server.ts

import app from './app'
const PORT=3001;

app.listen(PORT,()=>{
    console.log(`Express server listening on port ${PORT}`);
})

这是我的app.ts

import express from 'express' 
import {json,urlencoded} from 'body-parser'
import cors from 'cors'
import morgan from 'morgan'

class App{
    public app:express.Application;
    constructor(){
        this.app=express();
        this.config();
        this.app.get('/',(req,res)=>{
            res.send({message:'hello express!!!!!'})
        })
    }
    private config(){
        this.app.use(cors())
        this.app.use(json());
        this.app.use(urlencoded({extended:false}))
        this.app.use(morgan('dev'))
    }

}
export default new App().app

这是我的package.json

{
  "name": "node-framework-kit",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "dev": "ts-node-dev --respawn --transpile-only ./src/app.ts",
    "restart": "rimraf dist && npm run build && npm start",
    "start":"node ./dist/server.js",
    "prod":"npm run build && npm run start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.13",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongoose": "^6.0.5"
  },
  "devDependencies": {
    "@types/morgan": "^1.9.3",
    "morgan": "^1.10.0",
    "rimraf": "^3.0.2",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.4.2"
  }
}

这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "alwaysStrict": true,
    /* Module Resolution Options */
    "moduleResolution": "node",
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true, 
    "esModuleInterop": true,  
  },
  "include": [
    "./src/**/*"
  ]
}

4

1 回答 1

-1

完成!只需更改 package.json 中的脚本 "ts-node-dev --respawn --transpile-only ./src/app.ts" 是错误的,需要将其更改为 "ts-node-dev --respawn --transpile-只有 ./src/server.ts"

于 2021-09-08T09:22:29.580 回答