构建 AngularJS 包后,如何使用 NodeJS 作为后端请求服务器来部署 AngularJS
我的项目结构: 应用程序/ 应用程序/配置 应用程序/脚本 应用程序/样式 应用程序/ View 凉亭组件/ 距离/ Node 模块/ 测试/ 服务器.js
使用 grunt 构建 Angular 包后,我仅使用 dist 文件夹。但我想使用 Node 来处理我的请求,到服务器。那么如何在 dist build 文件夹中设置 nodejs 包。它将被部署到 parse.com
最佳答案
基本上,您需要一个简单的网络服务器,它始终返回index.html + 资源。
如果我位于您的根目录中(我猜此时是 /dist
),您必须创建一个包含以下内容的 app.js 文件:
var express = require('express');
var bodyParser = require('body-parser')
var path = require('path')
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(function (req, res, next) {
if (path.extname(req.path).length > 0) {
// normal static file request
next();
}
else {
// should force return `index.html` for angular.js
req.url = '/index.html';
next();
}
});
// static file serve
app.use(express.static(__dirname))
app.listen(3000)
关于angularjs - 构建 AngularJS 包后,如何使用 NodeJS 作为后端请求服务器来部署 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650614/