[英]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. 我正在使用Express开发一个NodeJS应用程序,我想将用户发送到app.post
函数中的另一个页面。
I know that you can serve pages by using something like this: app.use(express.static('client/home'));
我知道您可以使用类似以下内容的网页: 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
,页面仅显示“本地主机未发送任何数据”。
app.post('/createSchool', function(req, res) {
app.use(express.static('client/school'));
});
If I add res.end('');
如果我添加res.end('');
after app.use
, then the page doesn't crash but I'm met with a blank screen. 在app.use
之后,页面不会崩溃,但是遇到空白屏幕。
How can I get Express to serve the HTML in the client/school
folder? 如何获得Express以在client/school
文件夹中提供HTML?
I had to add a res.sendFile
after app.use
. 我必须在app.use
之后添加一个res.sendFile
。 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. **编辑:**当我在POST方法中删除app.use
(仅留给res.sendFile
,页面仍按预期工作。那么为什么我需要app.use
呢?我认为这是为所有就像使用res.sendFile一样,将文件夹中的所有文件(即HTML,CSS和JS)放在一起,只为您提供裸HTML。
I'm really confused now... 我现在真的很困惑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.