1app.use(methodOverride(function (req, res) {
2 if (req.body && typeof req.body === 'object' && '_method' in req.body) {
3 // look in urlencoded POST bodies and delete it
4 var method = req.body._method
5 delete req.body._method
6 return method
7 }
8}))
1'use strict';
2// Application dependencies
3const express = require('express');
4const cors = require('cors');
5const superagent = require('superagent');
6const pg = require('pg');
7const methodOverride = require('method-override');
8
9// Application setup
10require('dotenv').config();
11const app = express();
12const PORT = process.env.PORT || 3000;
13const client = new pg.Client(process.env.DATABASE_URL);
14app.use(cors());
15
16app.use(express.urlencoded({extended: true}));
17 app.set('view engine', 'ejs')
18
19app.use(methodOverride('_method'))
1using method-override in your index.js
21. npm install method-override --save
32. var methodOverride = require("method-override");
43. app.use(methodOverride("_method"));
5//'_method' is what methodOverride will look for
6method override in your UPDATE route:
7* <form action = "/campgrounds/<%=campground._id%>/edit?_method=PUT"
8 method = "POST">
9and EDIT routes:
10