title: Getting Error: Uncaught (in promise): Response with status: 404 Not Found for URL:
body: <p>I am making a request to endpoint written in koa.js and I am new to this framework.
  API return 404 response.</p>
  
  <p>Route file looks like below:</p>
  
  <pre><code>const collabRepo = require("../repo/collab/collab-repo")
  const notificationRepo = require("../repo/collab/notification-repo")
  const rest = require("restling")
  const jwt = require("jsonwebtoken")
  const CONFIG = require("config")
  const SECRET = CONFIG.get("auth.jwt.secret")
  const JWT_COOKIE_NAME = CONFIG.get("auth.jwt.cookie.name")
  const TOKENTIME = CONFIG.get("auth.jwt.ttl")
  const FEED_API_BASE = CONFIG.get("urls.activityFeedApi")
  const FEED_API_VERSION = "v1"
  const FEED_API = FEED_API_BASE + "/" + FEED_API_VERSION
  const logger = require("winston")
  const { jwtAuth } = require("../auth")
  
  const PROTECTED_ROUTES = [
      "/collab/follow",
      "/collab/unfollow",
      "/collab/follower/list",
      "/collab/follower/public/list",
      "/collab/following/list",
      "/collab/following/public/list",
      "/collab/following/count",
      "/collab/following",
      "/collab/notify",
      "/collab/discover/people",
      "/collab/discover/expression"
  ]
  
  async function followFeed(toFollowId, userObject, follow) {
      const args = {
          data: {
              follow: {
                  class: "user",
                  id: toFollowId
              }
          },
          headers: {
              "Content-Type": "application/x-www-form-urlencoded",
              "Cookie": JWT_COOKIE_NAME + "=" + jwt.sign(userObject, SECRET, {
                  expiresIn: TOKENTIME
              })
          }
      }
      if (follow) {
          return rest.post(FEED_API + '/follow', args)
      }
      return rest.post(FEED_API + '/unfollow', args)
  }
  </code></pre>
  
  <p>when I log rest.post(FEED_API + '/follow', args) I get:</p>
  
  <blockquote>
    <p>Promise {   _bitField: 0,   _fulfillmentHandler0: undefined,<br>
    _rejectionHandler0: undefined,   _progressHandler0: undefined,   _promise0: undefined,   _receiver0: undefined,   _settledValue: undefined } Unhandled rejection Error: Cannot POST
    <a href="http://localhost/activity-feed/api/v1/follow" rel="nofollow noreferrer">http://localhost/activity-feed/api/v1/follow</a>
        at Request. (\node_modules\restling\restling.js:26:21)
        at Request.emit (events.js:180:13)
        at Request._fireSuccess (\node_modules\restler\lib\restler.js:223:12)
        at \node_modules\restler\lib\restler.js:161:20
        at IncomingMessage.auto (\node_modules\restler\lib\restler.js:402:7)
        at Request._encode (\node_modules\restler\lib\restler.js:198:29)
        at \node_modules\restler\lib\restler.js:157:16
        at Request._decode (\node_modules\restler\lib\restler.js:173:7)
        at IncomingMessage. (\node_modules\restler\lib\restler.js:150:14)
        at IncomingMessage.emit (events.js:185:15)
        at endReadableNT (_stream_readable.js:1106:12)
        at process._tickCallback (internal/process/next_tick.js:178:19)</p>
  </blockquote>
  
  <pre><code>module.exports = (router) =&gt; {
      // Protect the marked routes
      PROTECTED_ROUTES.forEach(url =&gt; {
          router.use(url, jwtAuth)
      })
      // router.use("/collab", jwtAuth)
  
      router.post("/collab/follow", async function (ctx) {
          try {
              const resp = await followFeed(ctx.request.body.followee_id, ctx.state.user, true)
              return collabRepo.follow(ctx.state.user.id, ctx.request.body.followee_type, ctx.request.body.followee_id)
                  .then(data =&gt; {
                      ctx.body = {
                          data: data,
                          feed_data: resp.data
                      }
                  })
          } catch (error) {
              return error
          }
      }),
  
  } // end of the module
  </code></pre>
  
  <p>I have removed extra code which is not relevant to this question.</p>
  
  <p>Functions called within route:</p>
  
  <p><strong>Function followFeed</strong></p>
  
  <pre><code>async function followFeed(toFollowId, userObject, follow) {
      const args = {
          data: {
              follow: {
                  class: "user",
                  id: toFollowId
              }
          },
          headers: {
              "Content-Type": "application/x-www-form-urlencoded",
              "Cookie": JWT_COOKIE_NAME + "=" + jwt.sign(userObject, SECRET, {
                  expiresIn: TOKENTIME
              })
          }
      }
      if (follow) {
          return rest.post(FEED_API + '/follow', args)
      }
      return rest.post(FEED_API + '/unfollow', args)
  }
  </code></pre>
  
  <p><strong>Function follow</strong></p>
  
  <pre><code> follow: function(user_id, followee_type, followee_id) {
          return db("follower").returning("followee_id").insert({ "user_id": user_id, "followee_id": followee_id, "followee_type": followee_type })
              .then(data =&gt; {
                  return data[0]
              })
      },
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IKRmT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IKRmT.png" alt="Request"></a></p>
  
  <p>Can someone help me and tell me what is wrong with my code?
  I am signed in and session has JWT, route exist then why I am getting 404?
  What is wrong with code at backend?</p>
  
  <p>In browser console I get:</p>
  
  <blockquote>
    <p>Error: Uncaught (in promise): Response with status: 404 Not Found for
    URL</p>
  </blockquote>
  
  <p>This is what I get when I print  <code>console.log(rest.post(FEED_API + '/follow', args))</code></p>
  
  <pre><code>Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _progressHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined,
    _settledValue: undefined }
  Unhandled rejection Error: Cannot POST http://localhost/activity-feed/api/v1/follow
      at Request.&lt;anonymous&gt; (projectpath\node_modules\restling\restling.js:26:21)
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Below is activity feed route:</p>
  
  <pre><code>router.post("/v1/follow", function(ctx) {
      const user = ctx.state.user
      const request = ctx.request.body
      return feedRepo.followUserFeed(user.id, request.follow)
          .then(result =&gt; {
              ctx.body = result
          })
          .catch(error =&gt; {
              ctx.error = error
          })
  })
  </code></pre>
  
  <p>Function called inside this route </p>
  
  <pre><code>followUserFeed: async function(id, feed) {
      const userFeed = new GSFlatFeed(LOOKUP.FEEDS.USER, id)
      const feedToFollow = new GSFlatFeed(feed.class, feed.id)
      const res = await StreamProvider.followFeed(userFeed, feedToFollow)
      return res
  },
  </code></pre>
  
  <p>app.js of activity-feed/api</p>
  
  <pre><code>const Koa = require("koa")
  const Router = require("koa-router")
  const body = require("koa-body")
  const cors = require("kcors")
  const passport = require("koa-passport")
  const logger = require("winston")
  var JwtStrategy = require("passport-jwt").Strategy
  const CONFIG = require("config")
  const SECRET = CONFIG.get("auth.jwt.secret")
  const JWT_COOKIE_NAME = CONFIG.get("auth.jwt.cookie.name")
  
  const allRoutes = require("./server/routes/all-routes")
  
  const app = new Koa()
  const router = new Router()
  const jwtAuth = passport.authenticate(["jwt"], { session: false })
  
  passport.use(new JwtStrategy({
          secretOrKey: SECRET,
          jwtFromRequest: function(req) {
              var token = null
              if (req &amp;&amp; req.cookies) {
                  token = req.cookies.get(JWT_COOKIE_NAME)
              }
              return token
          }
      },
      function(jwt_payload, done) {
          done(null, jwt_payload)
      }))
  
  router.use("/v1/*", jwtAuth)
  
  allRoutes(router)
  
  app.use(async(ctx, next) =&gt; {
      try {
          await next()
      } catch (err) {
          logger.error(err)
          ctx.throw(err.status || 500, err.message)
      }
  })
  app.use(cors())
  app.use(passport.initialize())
  app.use(body())
  app
      .use(router.routes())
      .use(router.allowedMethods())
  
  module.exports = app
  </code></pre>
  
  <p>Any help would be highly appreciated!</p>