gpt4 book ai didi

javascript - 无法获取 Node js错误

转载 作者:行者123 更新时间:2023-12-03 06:27:27 25 4
gpt4 key购买 nike

我不知道我在哪里错过了添加内容。当我点击 portal.ejs 中的删除或更新图标时,出现以下错误:Cannot GET/portal/destroybox/5797318673cf3f581163455c:

(portal.ejs 中的createbox 工作并创建项目并将其存储在数据库中,然后我在表中 forEach显示每个项目后面带有更新和删除图标的所有项目。此外,server.js 中的 //todo test 部分适用于我的应用程序的另一部分,并且可以正常工作,与门户/框部分无关.我在 server.js 中也没有用于 createbox 的路由,但是该部分可以工作,那么如果我需要的话,为什么我还需要它用于 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/');
});
});
});

最佳答案

因为您没有错误指出的路线:

app.get('/destroybox/:id'

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

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

root/portal,然后destroybox,然后id。配置中没有定义。

<小时/>

解决方案是将href更改为:

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

其他 href 属性也是如此。

关于javascript - 无法获取 Node js错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com