JS我所做的EJS來呈現數據,並用jQuery的AJAX將數據發送到頁面,提醒它可能是這一意志對你有用
hello.js
var express = require('express');
var app = express();
var engine = require('ejs-locals');
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index',{user:"John Smith"})
});
app.get('/helo', function(req, res){
res.send({'data':'hello'});
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
index.ejs 目錄名爲意見---路徑應用程序的名字/視圖/ index.ejs
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("/helo", function(data, status){
console.log(data);
var result = data
alert(result['data'])
});
});
});
</script>
</head>
<body>
welcome <%= user%><br>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
你甚至使用任何模板語言,如ejs或玉? –
請在http://jsfiddle.net或http://codepen.io上添加代碼片段! –
@NomanUrRehman是的,它的ejs –