javascript - 发出 GET 请求时 Node 路由不起作用

标签 javascript node.js mongodb express

伙计们,我正在上 Node 和 Express 上的 udemy 类(class),但我陷入了困境。我正在使用 GET 方法 ruest 实现一条路由。该路线的形式为“campgrounds/:id”,但是上面有一条路线的名称为“/campgrounds/new”,这是提交表单。这是工作网络应用程序的链接。 以及工作应用程序的网页

http://webdevcamp-miatech.c9users.io/campgrounds

当您单击任何按钮“更多信息”时,它将重定向到“campgrounds/:id”,目前我只是测试路线并打印一些文本。 app.js 文件

var express = require("express"),
    app = express(),
    bodyParser = require("body-parser"),
    mongoose = require("mongoose");

//connect mongoose db
mongoose.connect("mongodb://localhost/yelp_camp");
app.use(bodyParser.urlencoded({extended: true})); //parsing html request
app.set("view engine", "ejs"); //handling vies through ejs pages

//schema/model for data to be inserted in db 
var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String
})

var Campground = mongoose.model("Campground", campgroundSchema);
Campground.create({
    name: "Salmon Creek", 
    image: "https://farm6.staticflickr.com/5786/20607281024_5c7b3635cc.jpg",
    description: "This is a huge granite hill, no bathroom campground"
}, function(err, campground) {
    if(err) {
        console.log(err);
    }
    else {
        console.log("Created Campground");
        console.log(campground);
    }
})

//default route
app.get("/", function(req, res) {
    res.render("landing");
})

app.get("/campgrounds", function(req, res) {
    Campground.find({}, function(err, allCampgrounds) {
        if(err) {
            console.log(err);
        }
        else {
            res.render("campgrounds", {campgrounds: allCampgrounds});
        }
    })
});

//method to add to database and redirect to campgrounds
app.post("/campgrounds", function(req, res) {
    // res.send("you hit post route");
    //get data from form and add to campgrounds array
    var name = req.body.name;
    var imgUrl = req.body.image;
    var newCampGround = {name: name, image: imgUrl};
    //adding to database table
    Campground.create(newCampGround, function(err, newlyCreated) {
        if(err) {
            console.log(err);
        }
        else {
            res.redirect("/campgrounds");
            console.log(newlyCreated);
        }
    });
});

app.get("/campgrounds/new", function(req, res) {
    res.render("new.ejs");
})

//displaying especific campground
app.get("campgrounds/:id", function(req, res) {
    //find campground with id
    // Campground.FindById(req.param)
    // res.render("single.ejs");
    res.send("This will be the single page!");
})


//starting server
app.listen(process.env.PORT, process.env.IP, function() {
    console.log("server started!..");
});

这是我在 c9.io 上的项目

https://ide.c9.io/miatech/webdevcamp

最佳答案

露营地前缺少斜线

app.get("/campgrounds/:id", function(req, res) {
    //find campground with id
    // Campground.FindById(req.param)
    // res.render("single.ejs");
    res.send("This will be the single page!");
})

关于javascript - 发出 GET 请求时 Node 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48350146/

相关文章:

node.js - 使用 NodeJs 读取大文件的最后一行

javascript - 为 React 项目设置 webpack

javascript - 需要一种方法来包含 Vue.JS 的内联文本编辑

javascript - 如何制作循环以重用函数

javascript - 如何获取当前登录用户authservice的用户名

javascript - 如何使用 Mongoskin 进行无序批量插入?

javascript - 有时JQuery ajax在重定向后没有执行

javascript - 如何连接到 "host:port"但不使用浏览器,仅使用 Node js/Javascript?

node.js - Mongoose - 查找包含子文档引用的文档

Ruby super 不敏感的正则表达式,用于将学校名称与重音符号和其他变音符号匹配