0

I'll try to be as clear as I can be. So I'm trying to make a little plateform. I would like that someone connect to the page (in localhost) and then, have the possibility to click on a button, and this button will redirect him to another html page.

So I have a project folder with my "initial" html, my app.js file, and the other html file, which should show itself when clicking on the button.

The server works like this :

    var application = require('express')(),
    server = require('http').createServer(application),
    io = require('socket.io').listen(server),
    ent = require('ent'),
    fs = require('fs');

application.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');

});

and then, I have the button :

<a href="pfc.html" target="_blank"> <input type="button" value="blah"></a>

When launching "node app.js" on the terminal, I go on the page, click on the the button and then I got the error "Cannot GET /pfc.html".

Sorry if I'm quite not understandable, I hope someone will understand me.

Thanks and have a nice day!!

3
  • 1
    Typo? It's sendFile (capital F) not sendfile. Capitalization matters. I'd be expecting you to see an error on the Node side when you did the GET, though. Commented Mar 23, 2017 at 10:48
  • pfc.html is a static file, you should search for "nodejs static file serving" and start there. Someone will probably answer with something similar, but you may be able to work it out yourself with that. Commented Mar 23, 2017 at 10:48
  • 1
    Are you sure you have pfc.html file, as your route for / is on the server that sends index.html, but where are you handling the click for pfc.html. Commented Mar 23, 2017 at 10:49

2 Answers 2

2

You wrote some code to tell your server what to do if the browser asks for /

application.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

Then you wrote a link:

<a href="pfc.html"

which makes the browser ask for /pfx.html.

You haven't written anything in the server side code to tell it how to respond to a request for that. You've only told it how to respond to a request for /.

You could write something similar (application.get('/pfx.html',...) or find some existing middleware for handling static files.

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

1 Comment

Ah yes. So the sendfile typo will be the next problem. :-)
0

If you have multiple HTML files like pfc.html, you can use

application.use(express.static('public'));

and keep all your html files (including pfc.html) in a folder named public and all those html files that you serve like:

<a href="pfc.html"></a>

you can look for them at public/pfc.html and such. If the file exists, then the application won't return stuff like Cannot GET /pfc.html and you won't need to write explicit routes for all of them.

2 Comments

It's working thank you! I'm feeling stupid not thinking about it earlier I was working on it for 3 hours!!
It happens to even the best of us @RomainEntakli :) I'm glad that you found the solution, I'm glad that I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.