我有写在angularJS和咕噜构建的应用程序。有没有一种方法,我可以创建一个从节点JS一个HTTP服务器和主机它。请分享任何代码片段或文件,这将有助于。谢谢
使用下面的代码在你的app.js
文件。
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
/* GET home page. */
app.get('/', function(req, res, next) {
//Path to your main file
res.status(200).sendFile(path.join(__dirname+'../public/index.html'));
});
module.exports = app;
使用app.js
运行node app.js
文件
这对我的工作例如与任何情况下,
即使你有基本的网址或不
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
/**
* This server should host angular appliation with or without base-url
* The angular static resources should be under `./public`
*/
var app = express();
app.use(function(req, res, next) {
console.log('Time:', Date.now() + ":", req.originalUrl)
next()
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/base-here-if-any', express.static(path.join(__dirname, 'public')))
app.get('*', function(req, res, next) {
//Path to your main file
res.status(200).sendFile(path.join(__dirname + '/public/index.html'));
});
const port = 3000
app.listen(port, () => console.log(`Example app listening on port ${port}!`))