Node express无法获取静态文件

时间:2017-11-06 07:47:53

标签: node.js express

这是我服务器的图片 https://drive.google.com/file/d/1GA57RyYsc5ik1pSlLhAGtgGjbp_vLFoH/view?usp=sharing

当我去http://localhost:3000/

我收到错误消息:无法获取/

myserver.js

arima(p,d,q)

4 个答案:

答案 0 :(得分:0)

每当您尝试访问浏览器上的任何网址时,浏览器会向该网址发出GET请求,如果您没有在网址上发送任何回复:“http://localhost:3000/。您可以尝试类似此

app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve("./client") + '/index.html'));
});

答案 1 :(得分:0)

检查您使用的命名,它会显示 myserver.js ,而不是 server.js ,就像您上传的图片一样。

检查代码第10行的路由

var lionRouter = require('./lions');
var tigerRouter = require('./tigers');

。试试这个编辑过的代码

server.js

// TODO: mount the tigers route with a a new router just for tigers
// exactly like lions below
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var _ = require('lodash');
var morgan = require('morgan');

var lionRouter = require('./server/lions');
var tigerRouter = require('./server/tigers');

app.use(morgan('dev'))
app.use(express.static('client'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// this is called mounting. when ever a req comes in for
// '/lion' we want to use this router
app.use('/lions', lionRouter);

app.use(function(err, req, res, next) {
  if (err) {
    res.status(500).send(error);
  }
});


app.listen(3000);
console.log('on port 3000');

答案 2 :(得分:0)

Express静态目录是给定客户端的,但它存在于父目录中。 所以我用路径模块解决了这个问题,现在这对你有用。



// TODO: mount the tigers route with a a new router just for tigers
// exactly like lions below
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var _ = require('lodash');
var morgan = require('morgan');
var path = require('path')

var lionRouter = require('./lions');
var tigerRouter = require('./tigers');

app.use(morgan('dev'))
app.use(express.static(path.join(__dirname, '../client')));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// this is called mounting. when ever a req comes in for
// '/lion' we want to use this router
app.use('/lions', lionRouter);

app.use(function(err, req, res, next) {
  if (err) {
    res.status(500).send(error);
  }
});


app.listen(3000);
console.log('on port 3000');




答案 3 :(得分:-2)

在对代码的以下三行进行注释后,您的代码可以正常工作:

var lionRouter = require('./lions');

var tigerRouter = require('./tigers');

app.use('/lions', lionRouter);

检查LionsJS中是否存在任何错误。