![](/img/trans.png)
[英]How to connect to a Node.js server that's using Express using an HTML file?
[英]Want to connect my html file with other html file while using node.js
我想创建一个具有 3 个链接的导航栏: Home ,About ,Contact 所有三个链接的内容都在不同的 html 文件中,不使用 node.js 它们相互连接并且运行良好但是在使用它时我的当前页面“主页”无法打开关于和内容页面。 请帮忙!
您需要首先按照以下代码设置您的节点服务器。 您可以替换您的 html 文件名。
var http = require('http');
var url = require('url');
var fs = require('fs');
var server = http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
switch (path) {
case '/':
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.write("This is Test Message.");
response.end();
break;
case '/About.html':
fs.readFile(__dirname + path, function (error, data) {
if (error) {
response.writeHead(404);
response.write(error);
response.end();
} else {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(data);
response.end();
}
});
break;
case '/Contact.html':
fs.readFile(__dirname + path, function (error, data) {
if (error) {
response.writeHead(404);
response.write(error);
response.end();
} else {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(data);
response.end();
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
response.end();
break;
}
});
server.listen(8082);
你直接使用这个代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.