NodeJS-res.sendFile提供html代码,但不加载/呈现该html [英] NodeJS - res.sendFile gives html code but not loading/rendering that html

查看:409
本文介绍了NodeJS-res.sendFile提供html代码,但不加载/呈现该html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对NodeJ非常陌生.我在NodeJS中使用express模块​​,并尝试通过sendFile命令加载html文件.在执行此操作时,我的第一个sendfile命令运行良好,但是对于第二个sendFile命令却无法运行.任何人都可以让我知道这里出了什么问题吗?

I am very new to NodeJs. I am using express module in NodeJS and trying to load html files through sendFile command. While doing it my first sendfile command works well, however it doesnt work well for second sendFile command. Can anyone please let me know whats going wrong here?

 var express = require('express');

 var app = express();
 var path = require('path');

 app.use(express.static(path.join(__dirname, 'public')));

 app.get('/', function(req, res){
   res.sendFile(path.join(__dirname, 'public/index.html'));
 });

 app.get('/unpack', function(req, res){
  res.sendFile(path.join(__dirname, 'public/main.html'));
 });

在上面的代码中,以下行效果很好

In the above code the below line works well

 res.sendFile(path.join(__dirname, 'public/index.html'));

问题在于

 res.sendFile(path.join(__dirname, 'public/main.html'));

推荐答案

感谢@ warl0ck和@robertklep的答复.正如@robertklep在评论部分中提到的那样,我的问题是我正在使用AJAX调用/unpack,因此我将响应作为html代码获得.这有助于我理解问题,并将AJAX更改为其他内容.

Thanks @warl0ck and @robertklep for your response. As @robertklep mentioned in the comments section my problem was that I was using AJAX to call /unpack hence I was getting the response as html code. This helps me to understand the problem and I will change AJAX to something else.

再次感谢@ warl0ck和@robertklep !!!

Thanks @warl0ck and @robertklep again!!!

-kt

这篇关于NodeJS-res.sendFile提供html代码,但不加载/呈现该html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆