如何在没有第三方库的情况下在服务器上以编程方式生成 Html
How can I programatically generate Html on the server, without a third party library?
我问了一个类似的问题,但在它作为副本关闭之前,我忘了提到我想了解如何在不使用第三方库的情况下做到这一点。
如何使用 Node.js 动态生成 Html 内容?
那么,如何使用 Node.js 在服务器端以编程方式生成 Html 内容呢?(我的意思不是发送硬编码的 Html,我的意思是像在客户端一样生成它)。
(我不反对第三方库。我更喜欢了解 Node.js在学习它之上的库之前
var http = require('http');
var html = '<!doctype html><html><body><h1>Hello, World!</h1></body></html>';
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
创建侦听您想要的任何端口的 Web 服务器,然后让它使用您想要的任何 html 响应对您想要的任何端点的请求。
// Include the HTTP Node library
// http://nodejs.org/docs/latest/api/http.html
var http = require('http');
// define the IP and port number
var localIP = "127.0.0.1"; // 127.0.0.1 is used when running the server locally
var port = 8080; // port to run webserver on
function sayHello(req, res) {
console.log("We've got a request for " + req.url);
// HTTP response header - the content will be HTML MIME type
res.writeHead(200, {'Content-Type': 'text/html'});
// Write out the HTTP response body
res.write('<html><body>' +
'<h1>Hello Dynamic World Wide Web<h1>'+
'</body></html>');
// End of HTTP response
res.end();
}
/************************/
/* START THE SERVER */
/************************/
// Create the HTTP server
var server = http.createServer(sayHello);
// Turn server on - now listening for requests on localIP and port
server.listen(port, localIP);
// print message to terminal that server is running
console.log('Server running at http://'+ localIP +':'+ port +'/');
上面的代码取自 https://gist.github.com/johnschimmel/1759465
相关文章:
- 我们可以使用任意的编程语言来动态化HTML页面吗
- 通过javascript以编程方式将文件插入HTML输入
- 如何以非编程方式国际化HTML文本
- 移动html/javascript编程中的变量传递
- 如果不使用HTML按钮,就无法通过编程方式触发SVG动画
- 在 html 页面上,javascript 是以编程方式操作 DOM 的唯一方法吗?
- 如何以编程方式在 HTML 页面上显示编辑文本字段
- 如何在没有第三方库的情况下在服务器上以编程方式生成 Html
- HTML:对按钮进行编程,使其具有点击功能,并同时使用 getmdl.io
- 以编程方式查看从另一个文件加载的 html 元素的内容
- Javascript:以编程方式构造 html 元素 ID
- 如何用javascript编程编辑HTML格式的文本,而不需要手动处理标记和HTML
- 以编程方式更改html页面的页眉信息
- 在磁盘上的新浏览器选项卡中使用javascript以编程方式打开html文件
- 删除以编程方式添加到HTML表中的最后一行表
- 在表单html上编程php和警告javascript
- 使用JavaScript编程编写html
- 编程一个石头剪刀html游戏
- javascript/html/其他编程语言的PDF文件
- 如何对HTML页面进行编程,以便在单击按钮时更新从文本文件导入的文本