webdevRefinery Forum: [Express] Route Trouble - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 10 August 2012 - 07:22 PM (#1)

[Express] Route Trouble


In my app file I have the following and it loads fine,

# Routes
app.get "/", routes.index
app.get "blog", routes.index
app.get "blog/posts/new", routes.newPost
app.post "blog/posts/new", routes.addPost
app.get  "blog/:id", routes.viewPost


routes is

# GET home page. 
exports.index = (req, res) ->
  res.render "index",
    title: "Random Blog",
    posts: []

exports.newPost = (req, res) ->
  res.render 'add_post', title: "Add a New Post"

exports.addPost = (req, res) ->
  post = req.body.post
  post.id = posts.length
  posts.push post
  res.redirect "/" 

exports.viewPost = (req, res) ->
  post = posts[req.params.id]
  res.render 'post', post: post, title: post.title


The problem is when I go to
GET /blog/posts/new
fill out the form and submit it
POST /blog/posts/new
I get:
Cannot POST /blog/posts/new
So what is my mubn mistake?
Reserved.
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 10 August 2012 - 07:28 PM (#2)

exports.newPost = (req, res) ->
  res.render 'add_post', title: "Add a New Post"


Never used coffee script before. But it appears that
title
is passed as a function parameter.
res.render()
's second argument should be an array.


Nevermind, reread the code. But I have never used coffee script so sorry.

In plain JS this would be:

exports.newPost = function(req, res) {
    res.render("add_post", {title: "Add a New Post"});
}

web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 10 August 2012 - 08:33 PM (#3)

View PostTheEmpty, on 10 August 2012 - 07:22 PM, said:

In my app file I have the following and it loads fine,

app.post "blog/posts/new", routes.addPost


exports.addPost = (req, res) ->
  post = req.body.post
  post.id = posts.length
  posts.push post
  res.redirect "/" 


The problem is when I go to
GET /blog/post/new
fill out the form and submit it
POST /blog/post/new
I get:
Cannot POST /blog/posts/new
So what is my mubn mistake?


Thats confusing basically your submitting it
POST /blog/post/new
which is not defined in your routes. notice the lack of an s You defined this:
app.post "blog/posts/new", routes.addPost

But that route has an s on posts
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 10 August 2012 - 11:22 PM (#4)

View PostTheEmpty, on 10 August 2012 - 07:22 PM, said:

# Routes
app.get "/", routes.index
app.get "blog", routes.index
app.get "blog/posts/new", routes.newPost
app.post "blog/posts/new", routes.addPost
app.get  "blog/:id", routes.viewPost


Apprently all routes must begin with a slash (and moreover, not end with a slash unless it's in the URL so /blog and /blog/ are different routes).

Fix:

# Routes
app.get "/", routes.index
app.get "/blog", routes.index
app.get "/blog/posts/new", routes.newPost
app.post "/blog/posts/new", routes.addPost
app.get  "/blog/:id", routes.viewPost

Reserved.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services