简体   繁体   English

路由在node.js中不起作用

[英]route is not working in node.js

campground data im working on this web app from a course where im using mongodb ,the database is created (named as "campit") and collection is named as campground(but mongo has named it campgrounds as usual) .the collection consist of name and image. 该营地数据在使用mongodb的过程中在此Web应用程序上运行,数据库已创建(命名为“ campit”),集合被命名为campground(但mongo照常将其命名为campgrounds)。集合包括名称和图片。 a route (namely "/create") is not working , by going to url "localhost:3000/create.ejs" its showing Cannot GET /index.ejs here is my code,this is app.js file 路由(即“ / create”)不起作用通过转到URL“ localhost:3000 / create.ejs”,其显示无法获取/index.ejs,这是我的代码,这是app.js文件

var express=require("express");
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/campit");
var campgroundSchema=new mongoose.Schema({
name:String,
image:String
});
app.get("/create",function(req,res){
campground.find({},function(err,campground){
  if(err){
    console.log(err)
  }
  else{
    console.log("successfully shown");
    res.render("index.ejs",{campgrounds:campground})
  }
});
app.listen("3000",function(){
  console.log("listening from server 3000");
});

this is index.ejs file,which is supposed to show 这是index.ejs文件,应该显示

    <div class="container">
  <div class="row no-gutters">
    <% campgrounds.forEach(function(camp){ %>
      <div class="col col-lg-4 img-thumbnail">
        <img class="but" height="75%" width="100%" src="<%=camp.image%>" alt="image">
        <div class="text-center">
          <h6><%= camp.name %> </h6>
          <button class="btn btn-outline-info " type="button" name="more info"> more info</button>
        </div>
      </div>
    <% }); %>
  </div>
</div>

EDIT 编辑

when im going to /create route it shows that campgrounds.forEach is not a function error......whole code is same, something is wrong with rendering variable..... and im sure that campgrounds contain data. 当我去/ create route时,它表明campgrounds.forEach不是函数错误……整个代码都相同,渲染变量.....出了点问题,并确保露营地包含数据。 EDIT CLOSE 编辑关闭

any kind of help will be highly appriciated. 任何帮助都将得到高度重视。 thank you..... 谢谢.....

You are hitting wrong url. 您输入的网址错误。 As you can see in you code. 如您在代码中所见。

app.get("/create", function(req,res){
    campground.find({},function(err,campground){
        if(err){
            console.log(err)
        }
        else{
            console.log("successfully shown");
            res.render("index.ejs",{campgrounds:campground})
        }
    }
});

Route you should be using is /create and not /create.ejs . 您应该使用的路由是/ create而不是/create.ejs index.ejs is the template rendered when you visit /create route index.ejs是您访问/创建路线时呈现的模板

Use this url localhost:3000/create instead of localhost:3000/create.ejs :) 使用此网址localhost:3000 / create而不是localhost:3000 / create.ejs :)

In your Javascript file, you wrote app.get('/create') . 在Javascript文件中,您编写了app.get('/ create') create.ejs its just the name of your template. create.ejs仅是模板的名称。 The path of your route and the name of your template doesn't need to be the same. 路线的路径和模板的名称不必相同。

Try using: 尝试使用:

var router = express.Router();
router.route('/create')
    .get(function(req,res){
 campground.find({},function(err,campground){
   if(err){
   console.log(err)
 }
else{
console.log("successfully shown");
res.render("index.ejs",{campgrounds:campground})
 }
});
  app.use('', router);

You forget to set view engine, use code below after app.use(bodyParser.urlencoded({extended:true})); 您忘记设置视图引擎,在app.use(bodyParser.urlencoded({extended:true}));之后使用下面的代码app.use(bodyParser.urlencoded({extended:true}));

USE IT 用它

app.set("view engine", "ejs");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM