浏览器下载文件而不是显示 html
Browser downloads the file instead of showing html
我已经在本地系统中开发了 MERN 应用程序。我正在尝试托管它 AWS-EC2(免费套餐)。
从来没有想过部署会如此痛苦。(好吧,我是 Node 的初学者。之前我使用过 php。我发现它更容易集成)
I referred this article 安装 Node 和 Express。节点安装成功。我使用以下代码创建了示例文件 'test.js':
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);
执行后
node test.js
浏览器下载了一个没有扩展名的文件。在编辑器中打开,里面有 'Hello World'。没有其他的。我很确定我已正确执行所有步骤。但我还是 'Downloading the file'
谁能帮我解决这个问题?
奖金问题:如何在 AWS EC2 中部署 MERN 应用程序?
祝你有愉快的一天
谢谢
试试这个(你忘记添加监听回调,内容类型有错别字):
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello world!\n');
}).listen(port, err => {
if (err) throw err
console.log('Listening on port',port);
})
为您的 html 页面使用适当的内容类型。
res.writeHead(200,{'Content-Type':'text/html'});
对于可见的 html 代码,应将 text/html
设置为内容类型而不是 test/plain
。
我已经在本地系统中开发了 MERN 应用程序。我正在尝试托管它 AWS-EC2(免费套餐)。 从来没有想过部署会如此痛苦。(好吧,我是 Node 的初学者。之前我使用过 php。我发现它更容易集成)
I referred this article 安装 Node 和 Express。节点安装成功。我使用以下代码创建了示例文件 'test.js':
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);
执行后
node test.js
浏览器下载了一个没有扩展名的文件。在编辑器中打开,里面有 'Hello World'。没有其他的。我很确定我已正确执行所有步骤。但我还是 'Downloading the file' 谁能帮我解决这个问题?
奖金问题:如何在 AWS EC2 中部署 MERN 应用程序?
祝你有愉快的一天 谢谢
试试这个(你忘记添加监听回调,内容类型有错别字):
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello world!\n');
}).listen(port, err => {
if (err) throw err
console.log('Listening on port',port);
})
为您的 html 页面使用适当的内容类型。
res.writeHead(200,{'Content-Type':'text/html'});
对于可见的 html 代码,应将 text/html
设置为内容类型而不是 test/plain
。