34051

app.post is not a function express node

I had all my routes in server.js but I wanted to make it modular and put into a folder called routes. I created a file called apis.js in routes folder but as I did that I get TypeError: app.post is not a function

server.js:

var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var urlencode = bodyParser.urlencoded({ extended: true}); app.use(express.static('public')); var apis = require('./routes/apis'); app.use('/', apis); module.exports = app;

apis.js:

module.exports = function(app){ app.get('/', function(req, res) { res.send('OK'); }); app.post('/idea', function(req, res) { ... }); };

Also, having module.exports = app in server.js is important as I have tests running and I want a instance of app everytime.

What am I missing?

Answer1:

Better approach :-

server.js

var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var urlencode = bodyParser.urlencoded({ extended: true}); app.use(express.static('public')); var apis = require('./routes/apis'); app.use('/', apis); module.exports = app;

apis.js :-

var router = require('express').Router(); router.post('/url',function(req,res,next){ //your code }) module.exports = router

Answer2:

You need to pass in your express app into your apis module so it can attach the routes to your app. If you want to use app.use to put your routes in a different root path, you can create another express router, and attach your routes to that, then app.use that router:

server.js:

var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var urlencode = bodyParser.urlencoded({ extended: true}); app.use(express.static('public')); var apis = express.Router(); require('./routes/apis')(apis); app.use('/', apis); module.exports = app;

Answer3:

There's a couple different methods for connecting your app to your routes, and it looks to me like you are mixing them together, resulting in your error.

The fix already mentioned...

var router = require('express').Router(); router.post('/url',function(req,res,next){ //your code }) module.exports = router

...works with the way you currently have your server.js file set up.

Another method that doesn't require you to modify your apis.js file is to require it in server.js using

require("./routes/apis.js")(app);

instead of

var apis = require('./routes/apis'); app.use('/', apis);

This ensures that the variable app is passed into the function in apis.js

The first version is recommended, but hopefully this explains why you are getting confused between the two, i.e. because the second is an alternate version.

See Differences between express.Router and app.get? for more information on why the router version is recommended.

Recommend

  • How to make my codeiginter password reset function secure?
  • In php, Prepare string and create XML/RSS Feed
  • Express: Req.body is undefined after POST req
  • AngularJS $http return value
  • grep: matching on literal “+”
  • The required anti-forgery cookie “__RequestVerificationToken” is not present. MVC 5
  • .htaccess mod rewriterule and ampersands
  • Express.js : POST data as KEY of a req.body object instead of VALUE of req.body?
  • Is it possible to run clang with llc flags
  • DIV instruction jumping to random location?
  • Adding elements to a huge XML file
  • Apache RewriteRule redirection with url encoded
  • Diff between two dataframes in pandas
  • Moving Android View and preventing onDraw to be called over and over again
  • xtable package: Skipping some rows in the output
  • How integrated is Collada to OpenGL ES
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • PHP CURL timing out but CLI CURL works
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Jackson Parser: ignore deserializing for type mismatch
  • Play WS (2.2.1): post/put large request
  • Meteor helpers not available in Angular template
  • sending/ receiving email in Java
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • PHP: When would you need the self:: keyword?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • JTable with a ScrollPane misbehaving
  • Memory offsets in inline assembly
  • unknown Exception android
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?