1

The code below is working

var express = require('express')
var app =  express();
var fs = require('fs')
var addUserToDB = require('./addUserToDB')

app.use('addUserToDB', addUserToDB)

app.get('/register.html', function(req,res){
    res.sendFile(__dirname+ "/" + "register.html");
})

var server = app.listen(8087,function(){

    console.log("Listening at 8087");
})


app.get('/addUserToDB',function(req,res){

    firstname = req.query.firstname;
    console.log(firstname)
})

app.get('/register.html', function(req,res){
    res.sendFile(__dirname+ "/" + "register.html");
})

However, when I try to remove the following method and place it into another .js file so I can get the firstName from that file. It's not working. The following code is in addUserToDB.js:

var addUserToDB = app.get('/addUserToDB',function(req,res){

        firstname = req.query.firstname;
        console.log(firstname)
    })
module.exports = addUserToDB;

I have tried making a addUserToDB.js file and added the code

var express = require('express')
var app = express();

app.get('addUserToDB',function(req,res){

    firstname = req.query.firstname;
    console.log(firstname)
})

but it seems I am missing something because it doesn't work. Thanks.

2
  • 1
    did you "require" the new file in your main server.js? Commented Feb 8, 2017 at 23:55
  • yes i have this: module.exports = addUserToDB; in my addUserToDB.js and also the following: var addUserToDB = require('./addUserToDB') app.use('addUserToDB', addUserToDB) in server.js Commented Feb 9, 2017 at 0:01

1 Answer 1

2

A few things here. First, need to do a require of addUserToDB.js from server.js (I will assume that's the name of your main file) and then use it as a middleware. Also, you need to export the app from addUserToDB.js.

server.js:

var express = require('express')
var app =  express();
var fs = require('fs')
var addUserToDB = require('./addUserToDB');

app.get('/register.html', function(req,res){
    res.sendFile(__dirname+ "/" + "register.html");
})

var server = app.listen(8087,function(){

    console.log("Listening at 8087");
})

app.use(addUserToDB);

addUserToDB.js:

var express = require('express')
var router = express.Router();

router.get('/addUserToDB',function(req,res){

    firstname = req.query.firstname;
    console.log(firstname)
})

module.exports = router;
Sign up to request clarification or add additional context in comments.

6 Comments

it's working thank you. I want to ask this: when you are doing module.exports = app; what, am i right in understanding that you are importing all usages of app in addUserToDB.js ?... and also, is there a way to export the entire file instead of a var or a function of a file?
Yes, you can say it like that. You're exporting the definition of the functions in your file, which in this case is app plus the route for /addUserToDB. Alternatively, you can use var router = express.Router(); instead of var app = express(); (and router instead of app) and you will achieve the same effect, but using Express's router is better than using app because you only export the route and not the whole app.
No, you can only export one variable. If you need to add more routes to addUserToDB.js you can call define more routes like router.get('/route') before exporting route (or app). Or in some cases, for controllers, you can just export an object with functions, like module.exports = { func1: function () { }, func2: function () {}};.
so its like the entire code inside a .js file belongs to a module.exports? - @Luiz
That would be the optimal way to do it. If you're separating your app into several modules or files, each module should do specifically one thing, so anything you do in a file and export should be related to the same thing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.