首页 > 解决方案 > RESTful 路由 - 无法获取我的“新”路由来发布(JavaScript、Node、Mongoose)

问题描述

我被困住和沮丧。希望有人可以帮助我。有人能告诉我为什么我的新 POST 路线不起作用吗?

我正在尝试创建一个笔记存档,其中每个类别都有不同的架构,所有这些都在 views 目录下的 rNotes 子目录中

这是文件夹结构..

这是我的 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

请帮忙!

标签: javascriptnode.jsrestexpress

解决方案


推荐阅读