Within my server.js file, the following code is present:
var path = require('path');
var express = require('express');
var app = express();
var htmlRoutes = require('./app/routing/routes.js')(app, path, express);
As for my route.js:
module.exports = function(app, path, express){
app.use(express.static(__dirname + '/../public'));
// I also attempted
//app.use('/', express.static(__dirname + '/../public'));
app.listen(10003, function(){
console.log('connected on 10003')
})
}
The message 'cannot GET /' keeps appearing on my browser. A look into my directory layout reveals the following structure:
dir main
-server.js
dir subMain
dir routing
-routes.js
dir public
-home.html
-list.html
My goal is to have 'node server.js' load and showcase the home.html page before routes.js. This is crucial as it allows me to utilize jQuery selectors in home.html, targeting a button class and triggering an AJAX call to '/list' to display list.html.
I experimented with the following approach:
module.exports = function(app, path, express){
app.use(express.static(__dirname + '/../..'));
app.use(function(request, response, next){
response.sendFile(path.resolve(__dirname + '/../public/home.html'));
})
app.listen(10003, function(){
console.log('connected on 10003')
})
}
However, I encountered this error:
Uncaught SyntaxError: Unexpected token <
Further attempts included utilizing multiple lines of express.static:
module.exports = function(app, path, express){
//loading the directory with the home.html
app.use(express.static(path.join(__dirname + '..', 'public')));
//loading directory with server.js by going up two level, from routing directory -> app -> main (has server.js)
app.use(express.static(path.join(__dirname + '..', '..')))
app.listen(10003, function(){
console.log('connected on 10003')
})
}
//Edit, added jquery event handler for button click on home.html
$(document).on('click', '.btn', sendSurvery);
function sendSurvery(){
var myQueryUrl = "http://localhost:10003/survey";
$.ajax({url: myQueryUrl, method: 'GET'}).done(function(response){
});
}
Despite these efforts, the issue persists where 'cannot GET /' is displayed. The necessity to showcase home.html first lies in loading the embedded jQuery library, enabling button selection and survey.html transmission upon a click event. An oversight was made regarding the placement of directories 'public' and 'routing,' which are housed within a subfolder named subMain.