如何与Node.js服务器中加载的HTML页面进行交互?

时间:2019-04-19 10:33:55

标签: javascript html node.js

我是Web开发的新手。我正在开发将与本地文件系统交互的应用程序。我了解我需要一个Node.js服务器:

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

fs.readFile('html1.html', (err, html) => {
    if(err){
        throw err;
    }

    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-type', 'text/html');
        res.write(html);
        res.end();
    });

    server.listen(port, hostname, () => {
        console.log('Server started on port: ' + port);
    });

});

我已经渲染了html1.html文件。假设html文件中有一个id为“ clickMe”的按钮。是否有一种方法可以使该按钮具有onlick事件,但是该事件在Node服务器文件中定义,而不是在html文件中定义? 我阅读了有关使用Express.js的方法,但是有什么方法可以避免这种情况? 预先感谢!

0 个答案:

没有答案