javascript - RESTful 路由 - 无法获取我的“新”路由来发布(JavaScript、Node、Mongoose)
问题描述
我被困住和沮丧。希望有人可以帮助我。有人能告诉我为什么我的新 POST 路线不起作用吗?
我正在尝试创建一个笔记存档,其中每个类别都有不同的架构,所有这些都在 views 目录下的 rNotes 子目录中
这是文件夹结构..
- jp_Res_Notes
-
node_modules
-
views
-
rNotes
-
tRex
-
tRex.ejs
-
new.ejs
这是我的 app.js
var express = require ("express"),
app = express(),
bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
var tRex = [
{name: "Big Girl", image:"https://images.unsplash.com/pxxxx", pms:"Dr Grant", cage:"North", price:".089", lease:"yes", weights:"+/-20", process:"aggressive! These guys know their stuff" },
{name: "Big Boy", image:"https://images.unsplash.com/pxxxx", pms:"L Dern", cage:"South", price:"1.5", lease:"yes", weights:"+/-10", process:"Hungry" },
{name: "Brother", image:"https://images.unsplash.com/XXX", pms:"Timmy", cage:"South", price:"1.5", lease:"yes", weights:"+/-10", process:"Thirsty" },
]
app.get("/", function(req,res){
res.render("landing");
});
app.get("/rNotes/tRex/tRex", function(req,res){
res.render("rNotes/tRex/tRex",{tRex:tRex});
});
app.post("/rNotes/tRex/tRex", function(req,res){
var name = req.body.name;
var image = req.body.image;
var pms = req.body.pms;
var cage = req.body.cage;
var price = req.body.price;
var lease = req.body.lease;
var weights = req.body.weights;
var process = req.body.process;
var newtRex = {name: name, image:image, pms:pms, cage:cage,
price:price,lease:lease,weights:weights,process:process}
tRex.push(newtRex);
res.redirect("rNotes/tRex/tRex");
});
app.get("rNotes/tRex/tRex/new", function(req, res){
res.render("/rNotes/tRex/new.ejs");
});
app.listen(process.env.PORT||3000, process.env.IP, function(){
console.log("The Jurassic Park Notes has started!!");
});
这是我的 new.ejs 文件
<%- include("../partials/header") %>
<h1>
New T Rex!
</h1>
<form action="/rNotes/tRex/tRex" method="POST">
<input type="text" name="name" placeholder="tRex Name">
<input type="text" name="image" placeholder="image url">
<input type="text" name="pms" placeholder="pms">
<input type="text" name="cage" placeholder="cage">
<input type="text" name="price" placeholder="price">
<input type="text" name="lease" placeholder="yes or no?">
<input type="text" name="weights" placeholder="weights">
<input type="text" name="process" placeholder="process">
<button>Submit!</button>
这是我的 tRex.ejs 文件
<%- include("../partials/header") %>
<h1>This is the T Rex page! And.. its not working!!</h1>
<% tRex.forEach(function(tRex){ %>
<div>
<h4><%= tRex.name %></h4>
<img src="<%= tRex.image %>">
<li><%= tRex.pms %></li>
<li><%= tRex.cage %></li>
<li><%= tRex.price %></li>
<li><%= tRex.lease %></li>
<li><%= tRex.weights %></li>
<li><%= tRex.process %></li>
</div>
<% }); %>
我知道我的发帖路线出了点问题。我认为这与使用子文件夹有关,但我非常感谢有人为我提供一些指导。
我收到这个错误
无法获取 /rNotes/tRex/rNotes/tRex/tRex
请帮忙!
解决方案
推荐阅读
- python-2.7 - 无法在树莓派 3b+ 上安装 tensorflow
- python - Can I pass data through two different django projects?
- python-3.x - 我怎样才能有 Normalizer 方法?
- java - Jdbc 模板在 DB 上找不到存储过程
- charts - bokeh 最新版本 (1.0.3) 中的高级图表在哪里?
- c++ - 将 DLL 附加到托管进程不起作用
- amazon-web-services - Cloudformation 中的循环列表
- javascript - Angular - 注册后重定向到登录并显示消息
- javascript - 没有正则表达式的字符串中的数字?
- r - 根据略有不同的列合并2个数据表