MEAN:Angular 应用程序的 nodejs 服务器 - 我如何提供 angular 路由
MEAN: nodejs server for Angular App - How do i serve angular routes
这是我的节点 server.js
它位于项目根目录中,具有自己的 npm 配置。所有 Angular 文件都在 /client
中,因此在 ng build
之后,dist 将在 client/dist
const express = require('express');
const colors = require('colors');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');
const PORT = process.env.port||'3200';
// init "app"
const app = express();
app.use(cors({origin: `http://localhost:4200`}));
// angular entry point
app.use(express.static(path.join(__dirname, 'client/dist')));
//parse incoming data before routes
app.use(bodyParser.json())
// api routes
app.use('/api',require('./api/api'));
// error middleware
app.use(function(err, req, res, next){
console.log(`${err}`.red.bold)
res.status(422).send({error: err.message });
});
// listen
app.listen(PORT, function(){
console.log(`app running on ${PORT}...`.magenta);
});
当我访问服务器时 http://localhost:3200/
我看到了我的 angular 应用程序。当我转到 http://localhost:3200/api/someExpressRoute
时,我得到了 api 函数。很棒
现在我需要弄清楚如何为 angular 路线提供服务。例如 http://localhost:3200/about
是我的 angular 单页应用程序的一部分。但是当我去那里时 url 服务器不知道该怎么做。
如何配置此服务器以将 http://localhost:3200/*
作为从索引提供的 angular 路由来处理?
以下是我如何通过 nodejs 服务我的 angular 应用程序:
var express = require('express'),
path = require('path'),
fs = require('fs');
var compression = require('compression');
var app = express();
var staticRoot = __dirname + '/';
var env = process.env.NODE_ENV || 'development';
app.set('port', (process.env.PORT || 5000));
app.use(compression());
/* other middleware */
/* place any backend routes you have here */
app.use(function(req, res, next) {
//if the request is not html then move along
var accept = req.accepts('html', 'json', 'xml');
if (accept !== 'html') {
return next();
}
// if the request has a '.' assume that it's for a file, move along
var ext = path.extname(req.path);
if (ext !== '') {
return next();
}
fs.createReadStream(staticRoot + 'index.html').pipe(res);
});
app.use(express.static(staticRoot));
app.listen(app.get('port'), function() {
console.log('app running on port', app.get('port'));
});
为应用程序提供服务时,确保所有前端 dist 文件都与此文件(我称之为 index.js
)位于同一文件夹中
这是我的节点 server.js
它位于项目根目录中,具有自己的 npm 配置。所有 Angular 文件都在 /client
中,因此在 ng build
之后,dist 将在 client/dist
const express = require('express');
const colors = require('colors');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');
const PORT = process.env.port||'3200';
// init "app"
const app = express();
app.use(cors({origin: `http://localhost:4200`}));
// angular entry point
app.use(express.static(path.join(__dirname, 'client/dist')));
//parse incoming data before routes
app.use(bodyParser.json())
// api routes
app.use('/api',require('./api/api'));
// error middleware
app.use(function(err, req, res, next){
console.log(`${err}`.red.bold)
res.status(422).send({error: err.message });
});
// listen
app.listen(PORT, function(){
console.log(`app running on ${PORT}...`.magenta);
});
当我访问服务器时 http://localhost:3200/
我看到了我的 angular 应用程序。当我转到 http://localhost:3200/api/someExpressRoute
时,我得到了 api 函数。很棒
现在我需要弄清楚如何为 angular 路线提供服务。例如 http://localhost:3200/about
是我的 angular 单页应用程序的一部分。但是当我去那里时 url 服务器不知道该怎么做。
如何配置此服务器以将 http://localhost:3200/*
作为从索引提供的 angular 路由来处理?
以下是我如何通过 nodejs 服务我的 angular 应用程序:
var express = require('express'),
path = require('path'),
fs = require('fs');
var compression = require('compression');
var app = express();
var staticRoot = __dirname + '/';
var env = process.env.NODE_ENV || 'development';
app.set('port', (process.env.PORT || 5000));
app.use(compression());
/* other middleware */
/* place any backend routes you have here */
app.use(function(req, res, next) {
//if the request is not html then move along
var accept = req.accepts('html', 'json', 'xml');
if (accept !== 'html') {
return next();
}
// if the request has a '.' assume that it's for a file, move along
var ext = path.extname(req.path);
if (ext !== '') {
return next();
}
fs.createReadStream(staticRoot + 'index.html').pipe(res);
});
app.use(express.static(staticRoot));
app.listen(app.get('port'), function() {
console.log('app running on port', app.get('port'));
});
为应用程序提供服务时,确保所有前端 dist 文件都与此文件(我称之为 index.js
)位于同一文件夹中