简体   繁体   中英

Node.js simple server having issues with embedded javascript in html file (routes.js)

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM