0

So I am trying to write a simple node server that will fetch a very simple html file when it is hit with a get request at the root directory. Everything works fine until I try to attach a javascript file to my html file, then I get hit with the error Uncaught SyntaxError: Unexpected token < I have realized that this is just a 404 error, but I am completely unable to figure out how to make this stop happening.

test.html

<!DOCTYPE html>
<html>
<head>
 <title>title</title>
</head>
<body>
<script type = "text/javascript" src = "/home/danny/Documents/Application/server/test.js"> </script>
booya
</body>
</html>

app.js

/**
 * Main application file
 */

'use strict';

var express = require('express');
var mongoose = require('mongoose');
var tweet = require('../models/tweet.js');

// Connect to database
mongoose.connect('mongodb://localhost/test');

// Setup server
var express = require('express');
var app = express();
require('./config/express');
require('./routes')(app);

app.engine('html', require('ejs').renderFile);

var server = require('http').createServer(app);

// Start server
server.listen(3000, function() {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

console.log(__filename);

// Expose app
exports = module.exports = app;

routes.js

/**
 * Main application routes
 */

'use strict';

module.exports = function(app) {

  // Insert routes below
  app.route('/aa')
    .get(function(req, res) {
      res.sendFile('/home/danny/Documents/Application/server/test.js')
    })

  // All other routes should redirect to the index.html
  app.route('/*')
    .get(function(req, res) {
      // res.set('Content-Type', 'text/html');
      res.render('/home/danny/Documents/Application/src/test.html');
    });
};

Anybody have any ideas why it is unable to find the file?

  • I think the issue is with your paths here, and with not leveraging the static file serving of Express. Check out this SO post: http://stackoverflow.com/questions/10434001/static-files-with-express-js – grammar Apr 21 '15 at 21:26

1 Answers1

0

I am going to take a long guess here, but maybe it's because you're doing this: /home/danny/Documents/<xyz>...?

This won't work because you're attempting to link to http://yourdomain.com/home/danny/Documents/<xyz>.

Try fixing this by putting a path that the web server will understand.

SysVoid
  • 584
  • 5
  • 18