我正在尝试发送带有令牌验证的电子邮件验证,该代码未标记我任何错误,但是当我使用自己的路线时,它不起作用,所以我不确定为什么,所有这些都是进入API开发,我要离开代码。
首先,我要保留服务器索引:
//IMports
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const NODE_ENV = process.env.NODE_ENV || 'development'
require('dotenv').config({path:__dirname+'/config.env'})
// Init app
const app = express();
// settings
app.set("port", process.env.PORT || 3000)
app.use(cors({
origin: process.env.PORT || 3003 //cors permite el uso de react
}))
app.use(morgan('dev'))//morgan debe dar información porcada petición
// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//routes
app.get("/", (req, res) => {
res.send("welcome to api kiral")
});
app.use(require('./routes/Task.routes'))
app.use(require('./routes/Team.routes'))
app.use(require('./routes/Register.routes'))
app.use(require('./routes/Prueba.routes'))
// Start server
app.listen(app.get("port"), () => {
console.log(`Server on port ${app.get("port")}`);
});
如何看到它正在使用path方法调用config.env,所以现在我向您展示该文档:
CLIENT_URL=https://localhost:3000
JWT_ACCOUNT_ACTIVATION = 6f192f5a5dfe77993d390ec486aa00317c8e6f63f7661181313eb551f7a7b4c34c60641f57e94ccabdd8d5be2fe07dfda21651473e0a39
JWT_SECRET = 7643d1abcc6b9b04a6e50510953a76abcb5d72e2fe3e04a074dd9871e390c6d1214db7af574c0afc82d292aa96217aa647a88d3cc8accd4
JWT_RESET_PASSWORD = 192366c6415126025d84ed1953a76abcb5d72e2fe3e04a074dd9871e390c6d1214db7af574c0afc82d292aa96217aa647a88d3cc8accd4
EMAIL_FROM = ree@grro.com
MAIL_KEY = SG.G-2g7t60To6KMnXJEpg23aALV1CgKGOLiRGkNEbbv10hI
邮件密钥是因为我使用的是SendGrid / mail,因此在这种情况下,所有这些env变量都可以正常工作。
如何在索引文档中看到我有路线,即下一个文档。
const { Router } = require('express')
const router = Router();
const authController = require("../controllers/Register.controller");
//TAREAS
//GETS
router.get("/register", authController.getAuth)
router.get('/register/:email', authController.getAuths)
module.exports = router;
至少,我有负责在收到用户电子邮件时发送电子邮件的代码。
const expressJwt = require('express-jwt')
const _ =require('lodash')
const { OAuth2Client } = require('google-auth-library')
const fetch = require('node-fetch')
const {validationResult} = require('express-validator')
const jwt = require('jsonwebtoken')
//Esto es para obtener el error de la base de datos, se puede personalizar para hacerlo amigable.
const { errorHandler} =require('../helpers/dbErrorHandling')
//el siguiente const se usará para enviar correos
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.MAIL_KEY)
const pool = require('../database/connection')
class AuthController {
async getAuth (req, res) {
const result = await pool.query('select User_email from user');
res.json({
code: 200,
message: "Prueba realizada con éxito",
data: result
});
}
async getAuths(req, res) {
const email = (req.params.email);
let sql = `select User_email from user where User_email = ${email}`;
const mail = poo.query(sql);
if(email.length = 0) {
return res.json({
code: 404,
message: "Equipo no encontrado",
data: [],
})
}else{
//generate token
const token = jwt.sign(
{email},
process.env.JWT_ACCOUNT_ACTIVATION,
{expiresIn: '1440m' }
)
const emailData = {
from: process.env.EMAIL_FROM,
to: email,
subject: 'Account activation link',
html: `
<h1>Please use the following to activate your account</h1>
<p>${process.env.CLIENT_URL}/register/${token}</p>
<hr />
<p>This email may containe sensetive information</p>
<p>${process.env.CLIENT_URL}</p>
`
}
sgMail.send(emailData).then(sent => {
return res.json({
message: `el email se ha enviado a ${email}`
})
}).cath(err => {
return res.status(404).json({
error: errorHandler(err)
})
})
}
}
}
const authController = new AuthController();
module.exports = authController;
我是使用APIS的新手,一切看起来都很好,所有已编码的库都已安装,所以没错,代码对我来说也很好。
答案 0 :(得分:1)
看起来像Cath的拼写错误,因为Cath可能会给您带来一些麻烦,以及我要评论的其他内容,请看一看,看看是否解决这些问题对您有帮助!