javascript - How to structure my project folder when building a secured NodeJs REST API -
i building rest api using nodejs , express, powered mongodb database. i've been struggling days trying right folder structure nailed down. far, can connect database , add new users without api, doing get, post, etc. requests. i've seen several tutorials online on how build api using node, none of them have more standardized way setting folder structure. , reason why having such hard time making work given current folder structure.
here folder structure
app ---models ------user.js ---api.js ---routes.js config ---auth.js ---database.js ---passport.js public views package.json server.js
server.js
// server.js // set ====================================================================== // tools need var express = require('express'); var app = express(); var port = process.env.port || 2016; var mongoose = require('mongoose'); var passport = require('passport'); var flash = require('connect-flash'); var configdb = require('./config/database.js'); // configuration =============================================================== mongoose.connect(configdb.url); // connect our database require('./config/passport')(passport); // pass passport configuration app.configure(function() { // set our express application app.use(express.logger('dev')); // log every request console app.use(express.cookieparser()); // read cookies (needed auth) app.use(express.bodyparser.json()); // information html forms app.use(bodyparser.urlencoded({ extended: true })); app.set('views', path.join(__dirname + '/views')); app.set('view engine', 'ejs'); // set ejs templating // set static files location /public/img /img users app.use(express.static(__dirname + '/public')); // required passport app.use(express.session({ secret: 'xxxxxxxxx' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash flash messages stored in session }); // routes ====================================================================== // require('./app/routes')(app, passport); // load our routes , pass in our app , configured passport // require('./app/api')(api, passport); app.use('/', require('./app/routes')(app, passport)); app.use('/api', require('./app/api')(api, passport)); // error handlers // catch unauthorised errors app.use(function (err, req, res, next) { if (err.name === 'unauthorizederror') { res.status(401); res.json({"message" : err.name + ": " + err.message}); } next(); }); // launch ====================================================================== app.listen(port); console.log('live on port ' + port);
api.js
var user = require('./models/user'); var express = require('express'); var apiroutes = express.router(); app.use('/api', apiroutes); module.exports = function(apiroutes, passport){ apiroutes.get('/testapi', function (req,res) { res.json({secretdata: 'abc123'}); }); }
every time hit endpoint /testapi
error "cannot /testapi"
think main issue how organize files , folder , import/require them right way. can me figure out?
server.js
on line app.use('/api', require('./app/api')(api, passport));
here telling express use ./app/api
middleware passing "api" , "passport" arguments.
where have defined api
variable ?
lets assume typo.. in case "app/api.js" exporting function , trying execute in server.js app.use('/api', require('./app/api')(api, passport));
returns undefined.
express expecting function middleware not return value function.
app/api.js
on line 4 have app.use('/api', apiroutes);
doesn't make sense, because api.js
has no idea "app".
cleanup server.js , api.js , try again
this tutorial might node express
Comments
Post a Comment