我已经通过" http://tanmaysarkar.com/html-form-with-ejs-template-in-nodejs/"这篇文章,但我已经写了逻辑
我正在使用EJS模板引擎的含义堆栈,我想将EJS形式的数据发布给Ctroller,该数据在Controller文件夹下,我不如何将其发布到控制器上,请帮助以下是EJS模板,login.ejs在Views Folder下
<%- include('partials/header') %>
<div class="form-container">
<h2>Login</h2>
<% if(locals.error) { %>
<div class="alert alert-danger"><%= error %></div>
<% } %>
<% if(locals.success) { %>
<div class="alert alert-success"><%= success %></div>
<% } %>
<form method="post" action="">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<%= locals.username || '' %>" class="form-control" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Login</button>
<a href="/register" class="btn btn-link">Register</a>
</div>
</form>
</div>
<%- include('partials/footer') %>
在此以下控制器的帖子上应称为
在控制器/login.controller.js中,我写了以下帖子req
router.post('/', function (req, res) {
// authenticate using api to maintain clean separation between layers
request.post({
url: config.apiUrl + '/users/authenticate',
form: req.body,
json: true
}, function (error, response, body) {
if (error) {
return res.render('login', { error: 'An error occurred' });
}
if (!body.token) {
//console.log("inside if loop " + body.token + "::" + req.body.username + "::" + body)
return res.render('login', { error: body, username: req.body.username });
}
// save JWT token in the session to make it available to the angular app
req.session.token = body.token;
// redirect to returnUrl
var returnUrl = req.query.returnUrl && decodeURIComponent(req.query.returnUrl) || '/';
console.log("return url" + returnUrl);
res.redirect(returnUrl);
//res.render('register')
});
});
我只需要添加
var ejs = require('ejs');
var login = require(__dirname + '/views/login.ejs');//for ejs
var logincontroller = require(__dirname + '/controllers/login.controller.js');//for ejs
" server.js"中的三行以上是我的应用程序的入口点,当我实际发布数据映射时,就像
一样app.use('/controllers/login.controller.js', logincontroller);//for ejs
和我的EJS文件
<form method="post" action="/controllers/login.controller.js">
需要