javascript - Understanding how to use NodeJS to create a simple backend -


i have been trying develop rather simple server in nodejs. basically, going simple api requires authentication (simple username/password style). not need kind of frontend functionality (templating etc.). problem is, can't seem head around approach of express/node.

specifically, questions are:

  • how wire in authentication? pass several handlers every route requires authentication, or there more elegant way this?
  • how express middleware (like app.use(express.bodyparser())) work? alter contents of request or response object? specifically, if use body parser (internally formidable?), access request data supposed parse?
  • when using authentication , have, say, credentials stored in database more information individual client associated, @ point extract information? i.e., when user logs in, fetch user record on login , pass on, or fetch in every handler requires information?
  • ultimately, know of open source application take at? i'd see has simple authentication , maybe utilizes formidable, since uploading file 1 of requirements.

as mentioned earlier, believe problem difficulty function-oriented approach in node (also, have rather limited experience in webservice programming). if know resource read on how approach architecting nodejs app, please don't hesitate point me it.

how wire in authentication? pass several handlers every route requires authentication, or there more elegant way this?

you should use session middleware. here pseudo code:

var http = require('http'); var app = express();  var authorize = function(req, res, next) {     if(req.session && req.session.appname && req.session.appname === true) {         // redirect login page         return;     }     next(); }  app.use(express.session()); app.all('/admin*', authorize, function(req, res, next) {  }); 

how express middleware (like app.use(express.bodyparser())) work? alter contents of request or response object? specifically, if use body parser (internally formidable?), access request data supposed parse?

every middleware have access request , response object. so, yes, modifies it. attach properties it. means inside handler (which middleware) may write:

if(req.body && req.body.formsubmitted && req.body.formsubmitted === 'yes') {     var data = {         title: req.body.title,         text: req.body.text,         type: req.body.type     }     // store data } 

when using authentication , have, say, credentials stored in database more information individual client associated, @ point extract information? i.e., when user logs in, fetch user record on login , pass on, or fetch in every handler requires information?

i think should things same way in other server side language. keep state of user (logged/not-logged) inside session. may keep user's id , fetch data him whatever need. depends of case, have ability cache information. because node not php example, mean it's not dieing.

ultimately, know of open source application take at? i'd see has simple authentication , maybe utilizes formidable, since uploading file 1 of requirements.

yep. wrote article simple mvc web site admin panel. available here. , code of here.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -