Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs: express, morgan, mongoose package

const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const Blog = require('./models/blog');

// express app
const app = express();

// connect to mongodb & listen for requests

const dbURI = "mongodb+srv://<username>:<password>@nodetutor.jpayl.mongodb.net/?retryWrites=true&w=majority";

mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(result => app.listen(3000))
  .catch(err => console.log(err));

// register view engine
app.set('view engine', 'ejs');

// middleware & static files
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use((req, res, next) => {
  res.locals.path = req.path;
  next();
});

// routes
app.get('/', (req, res) => {
  res.redirect('/blogs');
});

app.get('/about', (req, res) => {
  res.render('about', { title: 'About' });
});

// blog routes
app.get('/blogs/create', (req, res) => {
  res.render('create', { title: 'Create a new blog' });
});

app.get('/blogs', (req, res) => {
  Blog.find().sort({ createdAt: -1 })
    .then(result => {
      res.render('index', { blogs: result, title: 'All blogs' });
    })
    .catch(err => {
      console.log(err);
    });
});

app.post('/blogs', (req, res) => {
  // console.log(req.body);
  const blog = new Blog(req.body);

  blog.save()
    .then(result => {
      res.redirect('/blogs');
    })
    .catch(err => {
      console.log(err);
    });
});

app.get('/blogs/:id', (req, res) => {
  const id = req.params.id;
  Blog.findById(id)
    .then(result => {
      res.render('details', { blog: result, title: 'Blog Details' });
    })
    .catch(err => {
      console.log(err);
    });
});

app.delete('/blogs/:id', (req, res) => {
  const id = req.params.id;
  
  Blog.findByIdAndDelete(id)
    .then(result => {
      res.json({ redirect: '/blogs' });
    })
    .catch(err => {
      console.log(err);
    });
});

// 404 page
app.use((req, res) => {
  res.status(404).render('404', { title: '404' });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose schema index of multiple columns 
Javascript :: How to get prime numbers using for loop in Js 
Javascript :: stack array in localStorage 
Javascript :: javascript döngü dizisi 
Javascript :: set display size phaser 
Javascript :: calculate age from date of birth javascript 
Javascript :: find minimum 
Javascript :: phaser random rectangle 
Javascript :: phaser chained animation 
Javascript :: phaser play animation after delay 
Javascript :: test unitaire javascript 
Javascript :: data tables ajust columns after init 
Javascript :: nodejs where multiple condition findAll 
Javascript :: Total amount of points 
Javascript :: getauth firebase admin node.js 
Javascript :: Return characters in a string in alphabetic order 
Javascript :: decimal to hex 
Javascript :: javascript prompts user to input 
Javascript :: js includes case insensitive 
Javascript :: javascript basic programs 
Javascript :: reactnative timepicker 
Javascript :: console log like a pro 
Javascript :: how to change class by is in js by toggle 
Javascript :: javascript output a message into console 
Javascript :: javascript.loop 
Javascript :: convert excel file to json using node js 
Javascript :: javascript return data async 
Javascript :: express get port from request 
Javascript :: is there an api for netflix shows 
Javascript :: javascript xmlhttprequest 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =