我是ExpressJS和Node的新手。我试图将发布数据从index.js传输到main.js。下面是代码
index.js
// https://mvnrepository.com/artifact/org.springframework.retry/spring-retry
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.4.RELEASE'
main.js
var express=require('Express');
var app=express();
var things=require('./things.js');
var main=require("./main.js");
var bodyparser=require('body-parser');
var multer=require('multer');
var upload=multer();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(upload.array());
app.set("view engine","pug");
app.set("views","./views");
app.use("/login",main);
app.use("/things",things);
app.listen(2000);
login.pug
var express=require("express");
var router=express.Router();
var bodyparser=require('body-parser');
var multer=require('multer');
var upload=multer();
var app=express()
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(upload.array());
app.set("view engine","pug");
app.set("views","./views");
router.get("/",function(req,res){
res.render("login");
});
router.post("/",function(req,res){
console.log("Came to post");
console.log(req.body);
res.send("received");
});
module.exports=router;
我已经尝试了上述方法,并在提交电子邮件后收到“无法发布/”
我已经尝试过不使用路由器,而没有在index.js中编写所有get和post代码,但它可以正常工作,但在使用路由器时却无法使用。
答案 0 :(得分:0)
在login.pug文件中进行所需的更改,
form(action='/login', method='POST')