当我尝试启动nodejs
服务器时,它不会调用client/public
文件夹。当我指向http://localhost:3000
错误:未找到 /home/shankar/projects/apiDocs/server/server.js:36:12 at Layer.handle [as handle_request] (/home/shankar/projects/apiDocs/node_modules/express/lib/router/layer.js:95:5) 在trim_prefix (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:312:13) 在 /home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:280:7 在Function.process_params (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:330:12) 在下一个 (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:271:10) 在 /home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:618:15 在下一个 (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:256:14) 在Function.handle (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:176:3) 在路由器 (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:46:12)
下面是我的server.js代码
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
app.use(express.static(path.join(__dirname, "public")));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));
mongoose.connect('mongodb://127.0.0.1:27017/app', function (error) {
if (error) {
console.log(error);
}
});
app.all('/*', function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Access-Token,X-Key");
if(req.method === 'OPTIONS'){
res.status(200).end();
} else {
next();
}
});
app.all("/api/v1/*", [require('./middlewares/validateRequest')]);
app.use("/", require("./routes"));
app.use(function(req, res, next){
var err = new Error("Not found");
err.status = 404;
next(err);
});
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
我正在尝试在同一台服务器上运行我的客户端和我的其余服务,当我启动my server on port 3000
时,我希望客户端和服务器位于同一台服务器上。
文件夹结构:
/app
--node-modules
--public
--app //contains all angularjs related files
--assets //contains images and js libraries
--index.html
--server
--server.js
答案 0 :(得分:0)
尝试使用它:
app.use(express.static('./.'));
这应该让表达能够看到你的文件