gpt4 book ai didi

javascript - 使用带有 Node Express 的 Javascript 文件获取 net::ERR_ABORTED 404

转载 作者:行者123 更新时间:2023-12-02 22:20:22 25 4
gpt4 key购买 nike

我是 NodeJS 和 Express 编程新手。我正在尝试创建一个应用程序,它接受它处理的 odata 并创建一个表以显示在 EJS 创建的 index.html 页面中。我创建了默认的 Express 项目来修改它,我只添加了几行来检索脚本。当我启动应用程序时,此错误返回:

untitled:server Listening on port 3000 +0ms
GET / 304 16.825 ms - -
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\response.js:767:10)
at ServerResponse.contentType (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\response.js:595:15)
at ServerResponse.send (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\response.js:145:14)
at done (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\response.js:1004:10)
at tryHandleCache (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\ejs\lib\ejs.js:260:5)
at View.exports.renderFile [as engine] (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\ejs\lib\ejs.js:485:10)
at View.render (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\cgugliotta\WebstormProjects\untitled\node_modules\express\lib\application.js:592:3)
GET /stylesheets/style.css 304 1.331 ms - -
GET /odatafunction.js 404 3.561 ms - 1153
<小时/>

项目结构如下:

<小时/>

enter image description here

代码下方:

App.js

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname,'public','javascript')));
app.use(express.static(path.join(__dirname,'public','stylesheets')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// 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;

索引.js

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
res.sendFile('index.js');
});

module.exports = router;
<小时/>

索引.ejs

<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<script type="text/javascript" src="odatafunction.js">

var script;
var data = script.data;
$(document).ready(function () {
var html = '<table class="table table-striped">';
html += '<tr>';
var flag = 0;
$.each(data[0], function (index, value) {
html += '<th>' + index + '</th>';
});
html += '</tr>';
$.each(data, function (index, value) {
html += '<tr>';
$.each(value, function (index2, value2) {
html += '<td>' + value2 + '</td>';
});
html += '<tr>';
});
html += '</table>';
$('body').html(html);
});
</script>
</body>
</html>

我没能找到解决方案。你能帮助我了解问题所在吗?

最佳答案

这一行app.use(express.static(path.join(__dirname,'public','javascript')));看来您缺少 'javascripts's,因为您将静态 javascript 文件夹命名为 javascripts 而不是 javascript。您可能还需要将 /javascripts/ 添加到您的 src 路径引用中。

关于javascript - 使用带有 Node Express 的 Javascript 文件获取 net::ERR_ABORTED 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com