Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C9 Setup Express etc
# Open for more files
npm init
npm install express ejs body-parser --save
npm install mongoose --save
npm install passport passport-local passport-local-mongoose express-session --save
//=========//
// Setup! //
//=========//
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
//=========//
// Routes! //
//=========//
app.get("/", function (req, res){
res.send("127.0.0.1 Sweet 127.0.0.1");
});
app.post("/", function(req, res){
var inputFieldName = req.body.inputFieldName; //req.body is from bodyParser
res.redirect("/");
});
//===========//
// Listener! //
//===========//
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Project server has started: " + process.env.IP + ":" + process.env.PORT);
});
//=========//
// Setup! //
//=========//
var mongoose = require("mongoose");
//var Singular = require("./models/singular");
mongoose.connect("mongodb://localhost/databasename");
/*
* The following can go inside
* a modeles file that is required above
*/
//Define the database structure
var nameSchema = new mongoose.Schema({
key1: String,
key2: Number
});
/*
* Singular is a singular version of your collection name
* Automatically makes a collection with the plural version
* Model by convention is uppercase
*/
var Singular = mongoose.model("Singluar", nameSchema); //Use this if declared in app.js
module.exports = mongoose.model("Singluar", nameSchema); //Use this if in models file
//=================//
// Create Methods! //
//=================//
var newItem = new Singular({
key1: "String",
key2: 0
});
newItem.save(function(err, response){
if(err){} else ()
});
Singular.create({
key1: "String",
key2: 0
}, function(err, response){
if (err) {} else ()
});
//===============//
// Read Methods! //
//===============//
Singular.find({}, function(err, response){
if (err) {} else ()
});
Singular.find({key2: 0}, function(err, response){
if (err) {} else ()
});
//=========//
// Setup! //
//=========//
var passport = require("passport");
var LocalStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
app.use(require("express-session")({ //Express-session must be declared prior to passport information
secret: "Two people cannot keep a secret",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//To send user information on every route (declare after passport lines)
app.use(function(req, res, next){
res.locals.currentUser = req.user;
next();
});
//=========//
// Routes! //
//=========//
//uses custom middleware
app.get("/secret", isLoggedIn, function(req, res){
res.render("secret");
});
// Auth routes
app.get("/register", function(req, res){
res.render("register");
});
app.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){ //req.body is from bodyParser
if (err) {
console.log(err);
res.render("register");
} else {
passport.authenticate("local")(req, res, function(){
res.redirect("/secret");
});
}
});
});
// Login/Logout Routes
app.get("/login", function(req, res){
res.render("login");
});
app.post("/login", passport.authenticate("local", {
successRedirect: "/secret",
failureRedirect: "/login"
}), function(req, res){});
app.get("/logout", function(req, res){
req.logout();
res.redirect("/");
});
//=============//
// Middleware! //
//=============//
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
next();
} else {
res.redirect("/login");
}
}
var mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema({
username: String,
password: String
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment