所以我编写了一个表单service.html
<!DOCTYPE html>
<html lang="en">
<head>
<form action="http://localhost:8080/myaction" method="post">
<select>
<option value="get">GET</option>
<option value="post">POST</option>
<option value="del">DELETE</option>
<option value="put">PUT</option>
</select>
<input type="text" placeholder="URL"></input>
<input type="text" placeholder="STATUS"></input>
<input type="text" placeholder="TYPE"></input>
<textarea id="something" name="something" rows="14" cols="100" placeholder="Body"></textarea>
<div><input class="cbutton" type="submit" value="create"/></div>
</form>
</body>
</html>
,server.js文件是
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8080, function(){
console.log('Server running on 8080...');
});
app.js文件是
const port = 8080;
var express = require('express'),
app = express();
var bodyParser = require('body-parser');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: false;
}));
app.use(bodyParser.json());
app.get('/', function(req, res){
res.render('form');
res.sendFile("service.html"); //if html file is within public directory
});
app.post('/myaction', urlencodedParser, function (req, res) {
response = {
body:req.body.something
};
console.log(response);
res.end(JSON.stringify(response));
})
app.listen(port);
我有一个完整的bootstrap&amp;希望使用node.js服务器运行所有内容 当我从cmd行打开时,打开server.js文件&amp;运行localhost:8080 / service.html我希望身体中的部分打印出json表单作为响应 但是,当我提交它说不能发布/ myaction
帮助表示感谢。 nodejs新手
答案 0 :(得分:0)
html文件
<!DOCTYPE html>
<html lang="en">
<body>
<form action="/myaction" method="post">
<select>
<option value="get">GET</option>
<option value="post">POST</option>
<option value="del">DELETE</option>
<option value="put">PUT</option>
</select>
<input type="text" placeholder="URL"></input>
<input type="text" placeholder="STATUS"></input>
<input type="text" placeholder="TYPE"></input>
<textarea id="something" name="something" rows="14" cols="100" placeholder="Body"></textarea>
<div>
<input class="cbutton" type="submit" value="create"/> </div>
</form>
</body>
</html>
&#13;
const port = 8080;
var express = require('express'),
app = express();
var bodyParser = require('body-parser');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.get('/', function(req, res){
res.sendFile(__dirname + "/service.html");
});
app.post('/myaction', function (req, res) {
response = {
body:req.body.something
};
console.log(response);
res.end(JSON.stringify(response));
})
app.listen(port);
将此app.js文件作为节点应用程序运行,您不需要上面提到的server.js文件,并且工作正常。