Node.js部署:部署快递代码获取类模块未找到

时间:2019-07-10 21:20:36

标签: node.js typescript express deployment

编译打字稿文件后,在下一个estructure中获得js文件:

main directory
    public
        api
            controllers
                data-controller.js

    app.js
    package.json
    ...

app.json代码为:

Object.defineProperty(exports, "__esModule", {
    value: true
});

const express = require("express");
const bodyParser = require("body-parser");
const data_controller_1 = require("./public/apì/controllers/data-controller");
const cors = require("cors");

let dataController = data_controller_1.DataController.bootstrap();
const options = {
    allowedHeaders: ["Origin", "X-Requested-With", "Content-Type", "Accept", "X-Access-Token"],
    credentials: true,
    methods: "GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE",
    origin: "http://127.0.0.1:3000",
    preflightContinue: false
};

const main = express();
main.get('/', function (req, res) {
    res.send('hello world11');
});
main.use(cors());
main.options("*", cors(options));
main.use('/data', dataController.GetRouter());
main.use('/', function (req, res) {
    res.send('hello world11');
});
main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: false }));
//main.listen(5000);
main.listen(process.env.PORT || 3000, function () {
    console.log("SUCCESSFULLY");
});

package.json是:

{
  "name": "sb-admin-bs4-angular-8",
  "version": "7.0.0",
  "license": "Apache",
  "scripts": {
    "ng": "ng",
    "start": "node --harmony dist/src/server.js",
    "build": "ng build --configuration=production",
    "test": "ng test",
    "test-ci": "TEST_CI=true ng test",
    "e2e": "ng e2e",
    "web": "npm run server && npm run css && npm run html && npm run scss && npm run ico",
    "ico": "copyfiles src/**/*.ico dist",
    "html": "copyfiles src/**/*.html dist",
    "css": "copyfiles src/**/*.css dist",
    "server": "copyfiles src/**/server.js dist",
    "scss": "copyfiles src/**/*.scss dist",
    "clean": "npm cache clean --force",
    "lint": "ng lint"
  },
  "main": "./dist/server.js",
  "private": true,
  "dependencies": {
    "@types/cors": "^2.8.5",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.0.0",
    "express": "^4.17.1",
    "mongoose": "^5.6.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.800.0",
    "@angular/language-service": "8.0.0",
    "@types/jasmine": "2.8.9",
    "@types/jasminewd2": "2.0.5",
    "@types/node": "10.12.2",
    "codelyzer": "^5.0.1",
    "jasmine-core": "3.3.0",
    "jasmine-spec-reporter": "4.2.1",
    "karma": "^4.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "2.0.4",
    "karma-jasmine": "1.1.2",
    "karma-jasmine-html-reporter": "1.4.0",
    "protractor": "5.4.1",
    "ts-node": "7.0.1",
    "tslint": "5.11.0"
  },
  "engines": {
    "node": "~10.16.0",
    "npm": "~6.4.1"
  }
}

数据控制器代码为:

class DataController {

}
exports.DataController = DataController;

执行节点app.js时出现错误=> 错误:找不到模块'./public/apì/controllers/data-controller'

我为什么要得到它?我有什么解决方案?

1 个答案:

答案 0 :(得分:0)

// datacontroller file
class DataController {

}

module.exports = DataController;

// app.js file
const DataController = require("./public/apì/controllers/data-controller");

let dataController = new DataController();
dataController = dataController.bootstrap();