我对整个服务器工作都很陌生,我想自己做这件事。基本上,我有一个用js编写的带有express的服务器,我试图在我的html页面上打印从服务器发送的东西。
var app = require('express')();
var bodyParser = requires('body-parser');
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( {
extended: true;
}))
app.use(express.json());
var server = app.listen('7777');
console.log("It's on, m8!");
app.get('/', function(req, res) {
res.send("Sunny Filadelphia! ");
})
所以我想让“”出现在我的html页面上。有什么具体的教程我应该看吗?还是调用我的xmlhttp变量的特定方法?我真的很困惑。
发布于 2015-09-13 23:33:55
您将希望使用模板语言将内容插入到您已经获得的HTML中。车把和玉石都是Node的流行选择。
然后,一旦您正确地设置了它们(它们都有很好的文档),您可以
res.render('index', {
myString: "Sunny Filadelphia! "
});
以Jade为例:
div#putMyContentHere = myString
或者,您可以使用AJAX异步返回数据。在服务器上:
app.get('/data', function (req,res) {
res.send("Sunny Filadelphia! ");
});
以及jQuery的客户端:
$.get('/data', function (data) {
$("#myDiv").append(data);
});
发布于 2015-09-13 23:34:22
查看node.js文档中的文件系统API:https://nodejs.org/api/fs.html
我将研究writeFile方法在node.js中的应用。
var fs = require('fs');
fs.writeFile('index.html', 'Stuff to write', function (err) {
if (err) throw err;
console.log('Saved!');
});
https://stackoverflow.com/questions/32555512
复制相似问题