Tuesday 16 May 2017

app.post is not a function in node controller file

I am trying a login application in MEAN stack which redirects the user to login screen once hit the base URL. when I keep all my code in the main.js file (without controller) it works file. Now I am trying to implement MVC. So I redirect the '/' URL to usercontroller. It throwing an error app.post is not a function. Some point out the right way of doing this.

main.js

var config=require('./config'),
express = require('express'),
bodyParser = require('body-parser'),
moment = require('moment'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose'),
 hash = require('./pass').hash,
app = express(),
server = http.createServer(app),
later = require('later'),

mongoose.connect("mongodb://localhost/db");
require('./models/user')(mongoose);
var userController  = require('./controllers/userController');


app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/')));
server.listen(3001);

app.get('/login',userController);

app.get('/', function(req, res) {
return res.redirect('/login');
});

});

controllers/userController

var express = require('express');
var  http = require('http'),
path = require('path');
var mongoose = require('mongoose');
var User = mongoose.model('User');
var hash = require('../pass').hash;

var UserController = function(app,mongoose){

 app.get("/", function (req, res) {

    if (req.session.user) {
        res.send("Welcome " + req.session.user.username + "<br>" + "<a href='/logout'>logout</a>");
    } else {
        res.send("<a href='/login'> Login</a>" + "<br>" + "<a href='/signup'> Sign Up</a>");
    }
});

 app.get("/signup", function (req, res) {
    if (req.session.user) {
        res.redirect("/");
    } else {
        res.render("signup");
    }
});

 app.post("/signup", userExist, function (req, res) {
    var password = req.body.password;
    var username = req.body.username;

    hash(password, function (err, salt, hash) {
        if (err) throw err;
        var user = new User({
            username: username,
            salt: salt,
            hash: hash,
        }).save(function (err, newUser) {
            if (err) throw err;
            authenticate(newUser.username, password, function(err, user){
                if(user){
                    req.session.regenerate(function(){
                        req.session.user = user;
                        req.session.success = 'Authenticated as ' + user.username + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.';
                       return loadContacts(req, res, user);
                    });
                }
            });
        });
    });
});

app.get("/login", function (req, res) {

    console.log('test');
    res.end();
});

   app.post("/login", function (req, res) {
    console.log('login2');
    });
  });

  app.get('/logout', function (req, res) {
    req.session.destroy(function () {
        res.redirect('/');
    });
  });
 }
 module.exports = UserController;



via Sami

No comments:

Post a Comment