0

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:


enter image description here

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?

1 Answers1

2

this line app.use(express.static(path.join(__dirname,'public','javascript'))); it looks like you're missing an s for 'javascripts' since you named your static javascript folder javascripts not javascript. You might need to add /javascripts/ to your src path reference as well.

Peter Girnus
  • 2,673
  • 1
  • 19
  • 24
  • Thanks, 3 hours that i look at that code with no results. by the way now he load correctly the js, but i have browser side this error: Uncaught ReferenceError: require is not defined at odatafunction.js:1 express terminal side this: – claudio gugliotta Dec 09 '19 at 11:47
  • terminale side: GET / 304 51.297 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 ... i know that is a little bit different the question now but can you help me ? – claudio gugliotta Dec 09 '19 at 11:47
  • did you change the path of ` – Peter Girnus Dec 09 '19 at 11:50
  • maybe this will help you for your other issue: https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client? – Peter Girnus Dec 09 '19 at 11:51
  • For a while worked with – claudio gugliotta Dec 09 '19 at 12:39
  • I understand that in index.js he do not like the combo res.render() and res.sendFile(), but he still missing odatafunction.js – claudio gugliotta Dec 09 '19 at 13:00