【发布时间】:2019-11-11 04:14:53
【问题描述】:
我有这个代码:
let http = require('http');
let fs = require('fs');
let handleRequest = (request, response) => {
response.writeHead(200, {
'Content-Type': 'text/html'
});
fs.readFile('./index.html', null, function (error, data) {
if (error) {
response.writeHead(404);
respone.write('Whoops! File not found!');
} else {
response.write(data);
}
response.end();
});
};
http.createServer(handleRequest).listen(8000);
基本上,这段代码在 Node 上创建一个本地服务器并打开文件:'index.html'。
现在,我在我的“index.html”上创建了一个<button>,当被点击(onclick)时会调用一个名为“hello”的函数:
function hello() {
console.log('hello world);
}
所以,当单击按钮时,浏览器控制台中会显示“hello world”,但我希望 nodejs 控制台中显示“hello world”,而不是浏览器。
我怎样才能实现它?
谢谢!
【问题讨论】:
-
在
.js文件中创建一个 post/get 请求,然后从 html 调用它。 -
你能把代码发给我吗?我是菜鸟:(
-
对不起,我试过了,但我做不到
标签: javascript node.js browser server client