我正在尝试将我从主页javascript index.ejs传递的数据检索到routes / index.js中的函数
index.ejs
$(".user-create-btn").click(function(e){
$.ajax({
url: "/users/create/",
data: {username: "John123"},
type: "post",
dataType : "json",
success: function(result) {
console.log("test")
}
});
e.preventDefault(e);
location.reload();
});
路由/ index.js
/*Create new user*/
router.post('/users/create', (req, res) => {
//how can i retrieve the the data of the username here?
models.User.create({
username: username,
})
});
答案 0 :(得分:1)
var express = require('express')
, router = express.Router()
, bodyParser = require('body-parser')
var app = express();
app.use(bodyParser.json());
router.post('/users/create', (req, res) => {
var body = req.body,
username = body.username;
models.User.create({
username: username,
...
})
});
app.use(router);
var http = require('http');
var server = http.createServer(app);
server.listen(3000);