3

I have written a simple express script to serve a webpage with embedded javascript. However, the server can't seem to find any of the files that I am giving to it. What's more frustrating, sometimes it seems to work, only for it to break again when I change an irrelevant bit of code.

All of the files are where I am telling the script, but I constantly get the following kind of error:

GET http://localhost:55154/jsPsych/jspsych.js net::ERR_ABORTED 404 (Not Found)

Here is the express code:

// --- LOADING MODULES var express = require('express'); // --- INSTANTIATE THE APP var app = express(); // --- STATIC MIDDLEWARE app.use(express.static(__dirname + '/public')); app.use('jsPsych', express.static(__dirname + "/jsPsych")); // --- VIEW LOCATION, SET UP SERVING STATIC HTML app.set('views', __dirname + '/public/views'); app.engine('html', require('ejs').renderFile); app.set('view engine', 'html'); // --- ROUTING app.get('/', function(request, response) { response.render('index.html'); }); app.get('/experiment', function(request, response) { response.render('go_no_go.html'); }); // --- START THE SERVER var server = app.listen(process.env.PORT, function(){ console.log("Listening on port %d", server.address().port); }); 

And here is the relevant bit of javascript:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script type="text/javascript" src="../jsPsych/jspsych.js"></script> <link type= "text/html" href="jsPsych/css/jspsych.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="jsPsych/plugins/jspsych-html-keyboard-response.js"></script> <script type="text/javascript" src="jsPsych/plugins/jspsych-image-keyboard-response.js"></script> <script type="text/javascript" src="jsPsych/plugins/jspsych-survey-text.js"></script> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> </head> 
5
  • 1
    Why are you requesting ../jsPsych in the first <script> tag then ./jsPsych in the following ones? Where should the jsPsych directory be located on the disk? Should it be at the same level as your server script? Or maybe under public? Maybe somewhere else? Commented Jun 7, 2020 at 17:14
  • @rid That was a change I tried in but which didn't work, and forgot to change back. jsPsych should be at the same level as the server script. Commented Jun 7, 2020 at 17:24
  • Shouldn't then app.use('jsPsych', ...) be app.use('/jsPsych', ...)? Commented Jun 7, 2020 at 17:26
  • I suppose. I'm rather new to all of this. However, changing this doesn't fix the problem. Commented Jun 7, 2020 at 17:31
  • Seems to work for me. If I use your exact code but with app.use('/jsPsych', ...), http://localhost:55154/jsPsych/jspsych.js retrieves the file named jspsych.js under the directory jsPsych at the same level as the server script. Commented Jun 7, 2020 at 17:37

1 Answer 1

2

do you have to move up a directory to access jspsych.js? if not remove the ".." in your relative path to make sure that the relative path lines up with your files.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.