0
var express = require('express');
var router = express.Router();
var passport = require('passport');
var User = require('../models/user');
var Verify = require('./verify');
var request = require('request');
var response= require('response');
var fs = require('fs');
var formidable = require("formidable");
var util = require('util');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session')
var mongoose = require('mongoose');
//var nodemailer = require('nodemailer');
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
var async = require('async');
var crypto = require('crypto');
var flash = require('express-flash');
var flash = require('express-flash');
//var smtpTransport = require("nodemailer-smtp-transport");
mainRouter.use(session({secret:'session secret key'}));
mainRouter.use(flash());
mainRouter.use(bodyparser.json());
console.log('user made a fp');
mainRouter.route('/')
.get(function(request, response){
response.writeHead(200, {"content-type": "text;/html"})
fs.createReadStream("./homepage.html").pipe(response);
});
mainRouter.route('/register')
.get(function(request, response,next){
response.writeHead(200, {"content-type": "text;/html"})
fs.createReadStream("./signup.html").pipe(response);
var createHash = function(password){
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
};
passport.use('register', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password,done) {
findOrCreateUser = function(){
// find a user in Mongo with provided username
User.findOne({'username':username},function(err, user) {
// In case of any error return
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists');
return done(null, false,
req.flash('message','User Already Exists'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.username = username;
newUser.password = createHash(password);
newUser.email = req.param('email');
newUser.firstName = req.param('firstName');
newUser.lastName = req.param('lastName');
// save the user
newUser.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
// Delay the execution of findOrCreateUser and execute
// the method in the next tick of the event loop
process.nextTick(findOrCreateUser);
}));
.post(passport.authenticate('register', {
successRedirect: '/login',
failureRedirect: '/register',
failureFlash : true
}));
mainRouter.route('/login')
.get(function(request, response,next){
response.writeHead(200, {"content-type": "text;/html"})
fs.createReadStream("./login.html").pipe(response);
});
.post(function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.status(401).json({
err: info
});
}
req.logIn(user, function(err) {
if (err) {
return res.status(500).json({
err: 'Could not log in user'
});
}
var token = Verify.getToken(user);
/*res.status(200).json({
status: 'Login successful!',
success: true,
token: token
});*/
if(back=='/register')
return res.redirect ('/:mainId');
else
return res.redirect('back');
});
})(req,res,next);
});
mainRouter('/:mainId')
.get(Verify.verifyordinaryuser,function(request, response){
response.writeHead(200, {"content-type": "text;/html"})
fs.createReadStream("./homepage.html").pipe(response);
});
mainRouter('/:mainId/tr')
.get(Verify.verifyOrdinaryUser, function(request, response){
response.writeHead(200, {"content-type": "text;/html"})
fs.createReadStream("./tr.html").pipe(response);
});
module.exports = fpRouter;
錯誤插入如下: -
G:\EZY SCHOOLING BACKEND\expresswithmongodb\rest-server\routes\fpRouter.js:136
.post(passport.authenticate('register', {
^
SyntaxError: Unexpected token .
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (G:\EZY SCHOOLING BACKEND\expresswithmongodb\rest-server\app.js:42:14)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.2.1
npm ERR! npm v3.9.3
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the rest-server package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs rest-server
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls rest-server
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! G:\EZY SCHOOLING BACKEND\expresswithmongodb\rest-server\npm-debug.log
我創建電子商務像其網站後端nodejs和我使用的數據庫是mongodb,我創建了fpRouter.js這是顯示註冊的POST錯誤,請幫我解決這個問題
答覆謝謝,我知道錯誤是在這條線,但後功能無法正常工作,也沒有在它下面的一些線路的工作,在這個崗位功能整個文件不起作用 –
@MayankJain如果您不在'.post'之前放置路線,您的發佈功能將不起作用,這是錯誤告訴您的。解決這些'.post'錯誤後,我將需要更多信息或錯誤信息,以便爲您提供進一步的幫助。 – Zenny