无法GET节点js错误

时间:2016-07-26 10:09:42

标签: javascript node.js

我无法弄清楚我错过了哪些地方。当我点击Cannot GET /portal/destroybox/5797318673cf3f581163455c中的删除或更新图标时出现以下错误:portal.ejs

createbox中的portal.ejs工作并创建项目并将其存储在数据库中,然后我在表格中forEach显示所有项目,每个项目后面都有更新和删除图标。此外,server.js中的// todo test部分是针对我的应用程序的另一部分而且有效,与门户/盒子部分无关。我在server.js中也没有创建路由器的路由,但是那部分是有效的,那么如果我需要它,为什么我需要它为destroybox?)

portal.ejs

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Vm</th>
            <th>Update</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        <% boxes.forEach( function ( box ){ %>
            <tr>
                <td>
                    <%= box.box_name %>
                </td>
                <td>
                    <%= box.vm %>
                </td>
                <td>
                    <a class="fa fa-wrench" aria-hidden="true" href="/portal/editbox/<%= box._id %>" title="Update this box"></a>
                </td>
                <td>
                    <a class="fa fa-trash-o" aria-hidden="true" href="/portal/destroybox/<%= box._id %>" title="Destroy this box"></a>
                </td>
            </tr>
            <% }); %>
    </tbody>
</table>

<p>
    <strong>Add new box to `available boxes`</strong>
    <br>
</p>

<form action="/createbox" method="post">
    <div class="form-group">
        <label>Box name</label>
        <input type="text" class="form-control" name="box_name">
    </div>
    <div class="form-group">
        <label>VM</label>
        <input type="text" class="form-control" name="vm">
    </div>
    <div class="form-group">
        <label>Description</label>
        <input type="text" class="form-control" name="description">
    </div>
    <button type="submit" class="btn btn-warning btn-lg">Add box</button>
</form>

server.js

// server.js

// set up ======================================================================
// get all the tools we need

// mongoose setup
var db = require('./config/database.js');

require('./app/models/db');
require('./app/models/box');

var express = require('express');
var http = require('http');
var path = require('path');
var engine = require('ejs-locals');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var logger = require('morgan');
var errorHandler = require('errorhandler');
var static = require('serve-static');

var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');

var routesindex = require('./routes/index');
//var routesbox = require('./routes/boxi');


// configuration ===============================================================
mongoose.connect(db.url); // connect to our database

require('./config/passport')(passport); // pass passport for configuration

// set up our express application
app.engine('ejs', engine);
app.use(logger('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
app.use(session({
    secret: 'secretkeykeykeykey'
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

// routes ======================================================================
var routes = require('./app/routes/routes')(app, passport); // load our routes and pass in our app and fully configured passport

// todo test
app.use(routesindex.current_user);
app.get('/ind', routesindex.ind); 
app.post('/ind/create', routesindex.create);
app.get('/ind/destroy/:id', routesindex.destroy);
app.get('/ind/edit/:id', routesindex.edit);
app.post('/ind/update/:id', routesindex.update);

app.use(static(path.join(__dirname, 'public')));

// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);

routes.js

// =====================================
    // PORTAL ==============================
    // =====================================
    // show the signup form
    app.get('/portal', isLoggedIn, function (req, res) {
        var user_id = req.cookies ?
            req.cookies.user_id : undefined;

        Box.
        find({
            user_id: user_id
        }).
        sort('-updated_at').
        exec(function (err, boxes) {
            if (err) return next(err);

            res.render('portal', {
                boxes: boxes
            });
        });
    });

    app.post('/createbox', isLoggedIn, function (req, res) {
        new Box({
            user_id: req.cookies.user_id,
            box_name: req.body.box_name,
            vm: req.body.vm,
            description: req.body.description,
            updated_at: Date.now()
        }).save(function (err, box, count) {
            if (err) return next(err);

            res.redirect('/portal/');
        });
    });

    app.get('/destroybox/:id', isLoggedIn, function (req, res) {
        Box.findById(req.params.id, function (err, box) {
            var user_id = req.cookies ?
                req.cookies.user_id : undefined;

            if (box.user_id !== user_id) {
                return utils.forbidden(res);
            }

            box.remove(function (err, box) {
                if (err) return next(err);

                res.redirect('/portal/');
            });
        });
    });

    app.get('/editbox/:id', isLoggedIn, function (req, res) {
        var user_id = req.cookies ?
            req.cookies.user_id : undefined;

        Box.
        find({
            user_id: user_id
        }).
        sort('-updated_at').
        exec(function (err, boxes) {
            if (err) return next(err);

            res.render('editbox', {
                title: 'Vagrant Box',
                boxes: boxes,
                current: req.params.id
            });
        });
    });

    app.post('/updatebox/:id', isLoggedIn, function (req, res) {
        Box.findById(req.params.id, function (err, box) {
            var user_id = req.cookies ?
                req.cookies.user_id : undefined;

            if (box.user_id !== user_id) {
                return utils.forbidden(res);
            }

            box.box_name = req.body.box_name;
            box.vm = req.body.vm;
            box.description = req.body.description;
            box.updated_at = Date.now();
            box.save(function (err, box, count) {
                if (err) return next(err);

                res.redirect('/portal/');
            });
        });
    });

1 个答案:

答案 0 :(得分:1)

因为您没有路由,因为错误说明:

app.get('/destroybox/:id'

您可以看到您的路线从 root / destroybox 然后:id 等。当您有href:

 href="/portal/destroybox/<%= box._id %>" 

root /门户,然后 destroybox ,然后 id 。哪个在配置中没有定义。

解决方案是将href更改为:

href="/destroybox/<%= box._id %>" 

其他href属性也是如此。