오류 : 헤더를 보낸 후 설정할 수 없습니다. 나는이 문제를 싫어한다. 왜냐하면 여러 번 해보고 실수를 한 적은 있지만 잘못된 것이기 때문이다. 이 코드는 알았습니다.오류 : 헤더를 보낸 후 설정할 수 없습니다.
const mongoose = require ("mongoose");
const Spec = require("./specialist");
const Person = require("./person");
const Company = require("./company");
const bcrypt = require("bcryptjs");
module.exports.findUser=function(username,callback){
let query = {email_num:username};
Spec.findOne(query,(err_spec,spec_user)=>{
if(err_spec) throw err_spec;
if(!spec_user){
Person.findOne(query,(err_person,person_user)=>{
if(err_person) throw err_person;
if(!person_user){
Company.findOne(query,(err_company,company_user)=>{
if(err_company) throw err_company;
if(!company_user){
return console.log("Error User Not Found");
}
return callback(null,company_user);
});
}
return callback(null,person_user);
});
}
return callback(null,spec_user);
});
};
module.exports.comparePassword = function(candidatePassword, hash, callback){
bcrypt.compare(candidatePassword, hash, (err, isMatch) => {
if(err) throw err;
callback(null, isMatch);
});
};
module.exports.saveToken = function(username,role,token,callback){
let query = {email_num:username};
let updateToken={updatedToken:token};
if(role==="Person-User"){
Person.findOneAndUpdate(query,updateToken,callback);
}else if(role==="Specialist-User"){
Spec.findOneAndUpdate(query,updateToken,callback);
}else if(role==="Company-User"){
Company.findOneAndUpdate(query,updateToken,callback);
}else{
console.log("Something went goes wrong");
}
}
3 개의 모음과이 파일을 모두 처리했습니다. 이것이 내 메인 서버 코드입니다.
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const handlebars = require("express-handlebars");
const app = express();
const passport = require('passport');
const cookieParser = require("cookie-parser");
const config = require("./config/data");
const routes = require("./routes/users");
const company = require("./routes/company");
const person = require("./routes/person");
mongoose.Promise = global.Promise;
let options = {
useMongoClient: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
poolSize: 10,
bufferMaxEntries: 0
};
mongoose.connect(config.database,options);
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log(`DB connected ${new Date()}...`);
});
//app.set('views',__dirname+'views');
app.engine('handlebars', handlebars({defaultLayout:false}));
app.set('view engine', 'handlebars');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
// app.use(cors());
app.use(cookieParser());
// Passport Middleware
// require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.get("/",(req,res)=>{
res.render("index");
});
// app.get("/forgotPass",(req,res)=>{
// res.render("forgotPass");
// });
app.get("/user", (req,res)=>{
res.render("user");
});
app.get("/login",(req,res)=>{
res.render("login");
});
app.get("/signup",(req,res)=>{
res.render("signup");
});
app.get("/we",(req,res)=>{
res.render("we");
});
app.get("/blog",(req,res)=>{
res.render("blog");
});
app.get("/contactUs",(req,res)=>{
res.render("contactUs");
});
app.get("/userAsApplicant",(req,res)=>{
res.render("userAsApplicant");
});
app.use("/users",routes);
app.use("/company",company);
app.use("/person",person);
app.get("/faq",(req,res)=>{
res.render("faq");
});
app.listen(config.port,()=>{
console.log(`Server running on port ${config.port}....`);
});
또한 많은 양식 핸들러에서 모든 요청에 대해 Ajax를 사용하고 있습니다.
$(function() {
$('.subForm').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/users/spec/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
location.href="http://localhost:3000/signup"
}
}
});
e.preventDefault();
});
$('.personAuth').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/person/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
console.log("Chexav");
location.href="http://localhost:3000/signup";
}
}
});
e.preventDefault();
});
$('.companyAuth').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/company/register',
data: $(this).serialize(),
success:function(data){
if(data.success){
location.href="http://localhost:3000/login"
}else{
location.href="http://localhost:3000/signup"
}
}
});
e.preventDefault();
});
$('.logInForm').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'http://localhost:3000/users/authenticate',
data: $(this).serialize(),
success:function(data){
console.log(data);
if(data.token){
localStorage.setItem("Authorization",data.token);
$.ajax({
type:'get',
url:'http://localhost:3000/users/user',
beforeSend: function(xhr){xhr.setRequestHeader('auth', localStorage.getItem("Authorization"));},
success:location.href="http://localhost:3000/users/user"
})
}
}
});
e.preventDefault();
});
});
그리고 인증을위한이 경로. 내가 서버를 인증하기 위해 노력하고있어 때 템플릿 엔진으로
const express = require("express");
const router = express.Router();
const Spec = require("../models/specialist");
const jwt = require("jsonwebtoken");
const config = require("../config/data");
const Model = require("../models/model");
//Registration route
router.post("/spec/register",(req,res)=>{
let date=new Date();
let newUser = new Spec({
name:req.body.spec_name,
email_num:req.body.spec_email,
password:req.body.spec_password,
role:"Specialist-User",
isActive:true,
created:date,
updatedToken:"JWT"
});
if(newUser.password===req.body.spec_confirmPass){
Spec.getUser(newUser.email_num,(error,user)=>{
if(error) throw error;
if(!user){
Spec.addUser(newUser,(err,user)=>{
if(err){
console.log("err");
res.json({success:false,msg:"Somethings Went Wrong"});
} else {
res.header("Content-Type","application/json");
res.json({success:true,msg:"User Registered"});
// res.redirect("/login");
}
});
}else{
res.json({success:false,msg:"User Already Exists"});
}
});
}else{
res.json({success:false,msg:"Password Not Confirmed"});
}
});
//Authentication route
router.post('/authenticate', (req, res,next) => {
const email = req.body.email;
const password = req.body.password;
console.log("UserData");
Model.findUser(email, (err, user) => {
console.log("UserData1");
if(err) throw err;
if(!user){
return res.json({success: false, msg: 'User not found'});
}
Model.comparePassword(password, user.password, (err, isMatch) => {
console.log("UserData2");
if(err) throw err;
if(isMatch){
let payload={
name:user.name,
email:user.email_num,
role:user.role,
deleted:user.deleted,
isActive:user.isActive,
created:user.created,
};
let token = jwt.sign(payload,config.JWT_SECRET,{
expiresIn:1440
});
Model.saveToken(email,user.role,token,(err,success)=>{
if(err) return err;
console.log("Success");
// res.setHeader('Authorization',token);
// res.cookie('Authorization',token);
res.json ({ success: true, token: token });
// res.redirect("https://stackoverflow.com/users/user");
});
} else {
return res.json({success: false, msg: 'Wrong password'});
}
});
});
// res.redirect("/user");
});
router.use(function(req, res, next) {
console.log(req.headers);
let token = req.body.token || req.headers['auth'] || req.query.token || req.cookies.Authorization;
// console.log(token);
if (token) {
jwt.verify(token, config.JWT_SECRET, function(err, decoded) {
if (err) {
console.log(err);
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
req.decoded = decoded;
next();
res.render("user");
}
});
} else {
return res.status(403).json({
success: false,
message: 'No token provided.'
});
}
});
router.get("/user", (req,res)=>{
res.render("user");
});
module.exports = router;
내가 등록 모든 괜찮와 Handlebars.So 사용하지만거야 내가 쿠키를 사용할 수 있다는 것을 알고
Error: Can't set headers after they are sent.
제공합니다,하지만 난 원하는 그걸 막아라. 언어 실수로 불편을 끼쳐 드려 죄송합니다.
이 모든 것은 Ctrl + C 및 Ctrl + V 효과입니다. 매우 빠르고 효과적인 답변을 주셔서 대단히 감사합니다.))))))))))))))))))))))))) –