我是 Node.js 新手。我正在从 bin/wwww 运行 nodejs 并收到“jquery.min.js:4 GET http://127.0.0.1:3000/file.js 404 (Not Found)”错误。我正在一个简单的网页中尝试非常基本和简单的事情。只是尝试通过 ajax 调用在服务器上执行文件,但它总是显示文件未找到错误。
在我的 html 页面(firstpage.html)中,我正在尝试我想要的客户端请求。
<!DOCTYPE html>
<html>
<head>
<meta content="en-us" http-equiv="Content-Language">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="stylesheets/myButton.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var dataString;
$(document).ready(function(){
$.ajax({
type: "GET",
url: 'file.js',
data: dataString,
success: function(data){
console.log(data);
}
});
});
</script>
</head>
<body>
<p class="auto-style1"><strong>Welcome to MyPage</strong></p>
<div >
<input class="myButton" type="button" onclick="readTextFile(test.txt)" value="Submit" />
</div>
<button id="bb">QUERY</button>
</body>
</html>
我在服务器上有一个文件(file.js),
var fs = require("fs");
console.log("Going to write into existing file");
fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Let's read newly written data");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
});
当前 file.js 和 index.txt 位于 myapp\public\javascripts 位置。我的 app.js 如下,
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var firstpage = require('./routes/firstpage');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我在配置中犯了一些错误吗?请帮忙。
最佳答案
当请求 file.js 时,您的 express 永远不会被告知要提供什么服务。
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
因为它不是 .、/users 或/firstpage,所以它默认为 404 错误并返回该错误。而且因为是js文件,所以需要使用express的静态中间件功能。
app.use(express.static('routes');
应该是你想要的,我会把它放在后面
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
这样:
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
app.use(express.static('routes');
请注意,这将允许静态访问路由文件夹中的任何内容,因此您要么想要将所有静态资源移至其他位置,要么阻止路由文件夹中您不想访问的任何内容方式。
关于javascript - Node.js GET 404(未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686839/