使用node和ejs并获取用户详细信息时调用外部api端点 [英] Calling external api endpoints when working with node and ejs and geting the details for your users
问题描述
我有一个注册和登录表单,我使用ejs呈现为视图,我已经给了一些api端点来调用,并且我已经将它们添加到了他们的ejs表单操作视图中当我在ejs视图表单中填写用户详细信息时,会收到此响应.
i have a signup and login form,am using ejs to render as view, i have been given some api endpoint to call, and i have added them to their ejs form action views when i fill in the user details in my ejs view form i get this response.
我在尝试注册用户时得到了这个
i get this when am trying to register a user
{
"status": 200,
"message": "Account created successfully.",
"data": {
"name": "name of user",
"email": "user email",
"password": "$2b$10$0IPgBNCJhjjkasdkGb2y0CviOuxRe/bAfhFqFDqrofMaCz5f0SMtwkgBxIzm"
}
}
当我尝试登录注册用户时得到此提示
i get this when am trying to login a registered user
{
"status": 200,
"message": "Account logged in successfully.",
"data": [
{
"id": 9,
"name": "username",
"email": "useremail@gmail.com",
"password": "$2b$10$v3/zhn0pP6BMaTlfcoaMUuMlaHPGht6v2sf03e6uB3OPIQ3ggdpdi",
"date_created": "2020-02-21T13:15:33.000Z"
}
]
}
当我发布带有已注册用户详细信息的登录表单时,我会收到此消息
i get this when i post the sign in form with the details of an already registered user
{
"status": 400,
"message": "Provided email address already exists, try another",
"data": null
}
当我尝试使用错误的凭据登录时得到此提示
i get this when am trying to login with bad credentials
{
"status": 400,
"message": "Incorrect password provided, try again.",
"data": null
}
请问我如何才能访问此详细信息,以便可以在我的ejs视图中将其发送给客户端
please my question is how do i have access to this details so i can send them to the client in my ejs view
推荐答案
第一步安装axios(npm i axios)
First step install axios (npm i axios )
在文件夹(controllers/authController.js)中,编写此代码
In folder(controllers/authController.js),write this code
require('dotenv').config()
const request = require('request')
const axios = require('axios')
const createErrors = require('http-errors')
module.exports = {
callAPI: function(url, body, callback) {
axios.post(process.env.BASE_URL, body, {
headers: {
'cache-control': 'no-cache',
'Authorization': 'Token token=\"1234455433222222\"',
'Content-Type': 'application/json'
},
})
.then(function(response) {
console.log(response, "istheresponse");
callback(null, response)
})
.catch(function(error) {
if (error.response) {
callback(error.response)
// Request made and server responded
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
callback(error.request)
// The request was made but no response was received
console.log(error.request);
} else {
callback(error.message)
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
});
}
}
现在,创建路由文件夹(routes/Auth.routes.js)
Now,create routes folder (routes/Auth.routes.js)
require('dotenv').config()
const express = require('express')
const router = express.Router()
const axios = require('axios')
const authController = require('../controllers/Auth.controllers')
router.get('/', (req, res, next) => {
res.render('pages/auth/index')
})
router.get('/signin', (req, res, next) => {
res.render('pages/auth/signin', { title: 'website-name | Signin', value: 'Signin' })
})
router.post('/signin/', function(req, res) {
var data = {
"name": req.body.username,
"email": req.body.email,
"password": req.body.password,
}
console.log(data)
authController.callAPI(process.env.BASE_URL, data, function(error, result) {
if (error) {
var response = {
"status": 400,
"message": error,
}
res.render('pages/auth/signin', { response: result });
console.log(error)
} else {
// console.log(error, ' is the error');
// console.log(result, 'is the result')
if (result.data.status == 200) {
res.redirect('/');
} else {
res.render('pages/auth/signin', { response: result, title: 'website-name | Signin', value: 'Signin' });
}
}
});
});
module.exports = router
在(pages/auth/signin.ejs)文件夹中
In (pages/auth/signin.ejs) folder
<form id="signin" name="signin" class="default-form" action="/signin" method="post">
<div class="login-form">
<div class="sign-in-htm">
<div class="group">
<input id="user" type="text" name="name" class="input" placeholder="name">
</div>
<div class="group">
<input id="pass" type="password" name="password" class="input" data-type="password" placeholder="Password">
</div>
<div class="group">
<input id="pass" type="email" name="email" class="input" data-type="email" placeholder="email">
</div>
<div class="group">
<button type="submit" value="submit" class="button">Login</button>
</div>
</form>
这篇关于使用node和ejs并获取用户详细信息时调用外部api端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!