Skip to content

Instantly share code, notes, and snippets.

@jrmackie
Created April 27, 2017 13:40
Show Gist options
  • Save jrmackie/b74677d4c6c76440e0bba0c672f049dc to your computer and use it in GitHub Desktop.
Save jrmackie/b74677d4c6c76440e0bba0c672f049dc to your computer and use it in GitHub Desktop.
hello everyone, im new to node express mongo. the problem im facing is after posting my form i should be redirected back to root but insted i get a 404 can someone help
<!-- views/pages/about.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<% include ./partials/head %>
</head>
<body class="container">
<header>
<% include ./partials/header %>
</header>
<main>
<div class="row">
<div class="col-sm-8">
<div class="jumbotron">
<h1>This is great</h1>
<p>Welcome to templating using EJS</p>
<form action="/about" method="POST">
<input type="text" placeholder="name" name="name">
<input type="text" placeholder="quote" name="quote">
<button type="submit">Submit</button>
</form>
<ul class="quotes">
<% for(var i=0; i<quotes.length; i++) {%>
<li class="quote">
<span><%= quotes[i].name %></span>
<span><%= quotes[i].quote %></span>
</li>
<% } %>
</ul>
</div>
</div>
<div class="col-sm-4">
<div class="well">
<h3>Look I'm A Sidebar!</h3>
</div>
</div>
</div>
</main>
<footer>
<% include ./partials/footer %>
</footer>
</body>
</html>
var express = require('express');
var router = express.Router();
const MongoClient = require('mongodb').MongoClient
var dburl = "mongodb://******:***********@ds151060.mlab.com:51060/act1db";
var db;
MongoClient.connect(dburl, (err, database) => {
if (err) return console.log(err)
db = database
console.log('we are connected to mongolabs');
})
/* GET home page. */
router.get('/', (req, res) => {
db.collection('quotes').find().toArray((err, result) => {
if (err) return console.log(err)
// renders about.ejs
res.render('about.ejs', {quotes: result})
})
})
router.post('/about', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('index.ejs')
})
})
module.exports = router;
const express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
var index = require('./routes/index');
var about = require('./routes/about');
var users = require('./routes/users');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// trail post
app.use('/', index);
app.use('/about', about);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment