简体   繁体   中英

Change static folder - express

I'm making a NodeJS app with express, and I would like to send the user to another page in an app.post function.

I know that you can serve pages by using something like this: app.use(express.static('client/home')); and this is working in my app, but when I put this into my app.post as follows below, the page just says "localhost didn't send any data".

app.post('/createSchool', function(req, res) {
  app.use(express.static('client/school'));
});

If I add res.end(''); after app.use , then the page doesn't crash but I'm met with a blank screen.

How can I get Express to serve the HTML in the client/school folder?

I had to add a res.sendFile after app.use . The function is now as follows:

app.post('/createSchool', function(req, res) {
  app.use(express.static('client/school'));
  res.sendFile(path.join(__dirname, '/client/school/index.html'));
});

The reason I didn't add this is because it wasn't needed at the start. Why is this, by the way?

**Edit: ** When I remove the app.use in the POST method (leaving me with just res.sendFile , the page still works as intended. So why do I need app.use ? I thought it was to serve all the files in the folder together, ie HTML, CSS and JS, as just using res.sendFile just gives you the bare HTML.

I'm really confused now...

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