in parse server, should put express routes? found work if put them straight in index.js, can't best place, it?
i put in cloud/main.js:
var express = require('express'); var app = express(); app.listen(); app.get('/test', function(req, res) { console.log("working?"); res.status(200).send('working?'); }); console.log("this file runs");
my console output shows "this file runs" on starting server, when try access localhost:1337/test, says "cannot /test". whereas if put app.get('/test', ...); in index.js, works. suppose it's because i'm not allowed create express() instance, maybe there's way instance created in index.js?
i realise asking different question answer pointing to. in case still forgot app.listen
i'll leave below.
if you're going export routes different file you'll need pass around app
object index.js
routes file.
what 3 levels of abstraction. firstly, have index.js
declares app
, express instances
. have seperate file, routedefinitions.js
(i call index , name starting point after application).
inside routedefinitions.js
declare routes using app
instance index.js
resulting in following:
app.js
:
var express = require('express'); var app = express(); var routes = require('./routedefinitions')(app);
and routedefinitions.js
:
var test = require('./test'); module.exports = function(app) { app.get('/test', test.working); return app; };
and have each type of object in it's own route file (say have users, apples , cars each of objects has it's own routes file).
test.js
:
module.exports.working = function(req, res) { console.log("working?"); res.status(200).send('working?'); };
need start server calling
app.listen
see hello world example. you see this runs
because indeed processing script, not listening requests because forgot start express server.