我想使用restify和post route打印输入值,但它不起作用。将打开form.html页面,提交后,我会在URL中看到:
http://localhost:8081/?name=dsf
但我在控制台中看不到任何消息。我做错了吗?
这是我的代码:
select.js
var restify=require('restify');
var server=restify.createServer();
var mysql=require('mysql');
server.listen(8081, function(){
console.log("%s is running at %s", server.name, server.url);
});
var pool=mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'books'
});
server.get(/\/?.*/, restify.serveStatic({
directory: __dirname,
default: 'form.html'
}));
server.post('/hello/', function send(req, res, next){
pool.getConnection(function(err, connection){
if(err){
}else{
var table=req.body.name;
console.log(table);
}
});
});
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="POST">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
这两个文件都在C:/ restify文件夹中。
答案 0 :(得分:1)
action
代码中缺少 <form>
属性。尝试将其设置为action="/hello/"
。它能解决你的问题吗?