0

I am making RESTful BlogApp and PUT method is not working.I only get error saying Cannot PUT /blogs/:id. I don't see what could go wrong. Everything looks fine to me. Please let me know if you find a problem. I know this might be a bad post but it is one of my first posts. Here is what i coded:

<form action="/blogs/<%= blog._id %>?_method=PUT" method="POST" class="ui 
form">
            <div class="field">
                <label for="blog[tittle]">Tittle</label>
                <input name="blog[tittle]" type="text" value="<%= 
blog.tittle %>">
            </div>
            <div class="field">
                <label for="blog[image]">Image URL</label>
                <input name="blog[image]" type="text" value="<%= blog.image 
%>">
            </div>
            <div class="field">
                <label for="blog[body]">Content</label>
                <textarea name="blog[body]"><%= blog.body %></textarea>
            </div>
        <button type="submit" class="ui blue button">Save</button>
    </form>


const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
const mongoosePassword = 'bm9kZS1yZXN0LWJsb2c=';

mongoose.connect('mongodb://mesopotamija9:' + mongoosePassword + '@node- 
rest-blog-shard-00-00-xb2tn.mongodb.net:27017,node-rest-blog-shard-00-01- 
xb2tn.mongodb.net:27017,node-rest-blog-shard-00-02- 
xb2tn.mongodb.net:27017/test?ssl=true&replicaSet=node-rest-blog-shard- 
0&authSource=admin&retryWrites=true', {useNewUrlParser: true});

app.set('view engine', 'ejs')
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride('_method'));


app.put('blogs/:id', function(req, res){
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function(err, 
updatedBlog){
    if (err) {
        res.redirect('/blogs');
        console.log(err);
    } else {
        res.redirect('/blogs/' + req.params.id);
        console.log(updatedBlog);
    }
    });
});
  • I don't think `_method=PUT` is sufficient, that's still a POST – michelem Jan 02 '19 at 11:48
  • Possible duplicate of [Using PUT method in HTML form](https://stackoverflow.com/questions/8054165/using-put-method-in-html-form) – DaFi4 Jan 02 '19 at 13:47

0 Answers0