I'm new to programming with NodeJS and Express. I am trying to make an application that takes an odata it processes and creates a table to show in the index.html page created by EJS. I created the default express project to modify it, I added only a few lines to retrieve the script. When I launch the application this error returns:
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
The structure of the project is as follows:
Below the code:
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;
Index.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;
index.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>
I'm not managing to find a solution. Can you help me somehow to understand what is wrong?