node.js - 如何直接在 Angular 4/expressJS 服务器中运行项目(无需 webpack)

标签 node.js angular express webpack

现在我执行“ng build”和执行“ts-node ./src/index.ts”:

//////////index.ts
    import * as express from 'express';
    import { join } from 'path';
    import { json, urlencoded }from 'body-parser';
    import BaseRoutes = require("./backend/services/newService");

    var app : any = express();

    app.use(express.static(join(__dirname + '/../dist/')));
    app.use(json());
    app.use(urlencoded({extended: true}));
    app.use('/api', new BaseRoutes().routes);
    app.get('*', function(req, res, next) {
    	res.sendFile(join(__dirname + '/../index.html'));
    });
    app.listen(3000);

一切都很好,但是如何仅运行expressJS服务器,仅使用原始文件(没有webpack)编译项目。

如果要推送产品,我仅通过压缩的/dist/files 来编译项目,否则我想使用原始文件

varibale __dirname 是

C:\Users\AxOn\Documents\projects\techStarters\ang4Express\src

请帮忙

如果我只执行 'ts-node ./src/index.ts' 并将路径更改为 __dirname + '/index.html' 我会看到白色显示: enter image description here

似乎 Angular 未加载。

最佳答案

在你的 package.json

{
  "name": "project-name",
  "version": "0.1.0",
  "private": true,
  "scripts": {
  "start": "node ./server.js"
  },
  "dependencies": {...

然后在你的项目目录中的命令行中执行;

npm start

npm 将执行 package.json 文件开始部分中的任何内容

关于node.js - 如何直接在 Angular 4/expressJS 服务器中运行项目(无需 webpack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45820068/

相关文章:

javascript - 使用 crontab 在服务器上运行 Javascript 文件

node.js - 运行 Node 应用程序时未捕获、未指定的 'error'

node.js - Azure Function 因错误而挂起

javascript - Express 应用程序不听没有端口

angular - 如何修复排序、限制和跳过不适用于大量数据的问题

javascript - Change Detection 过程以及检查 OnPush 属性的确切时间

javascript - 我如何使用 Express/Mongoose 将模型引用到我的用户模型

css - Angular ngClass mat-list-item 添加条件以应用背景色

node.js - 使用 bookshelf.js,如何自动更新模型记录?

file-upload - 如何在 sails 中指定 Express 的文件上传目录?