You could use any of those tutorials.
But do not implement the template stuff.
Use expressjs to handle requests.
For an request you get the request object (req) and the response object (res).
Passport is something like a middleware -> something that is executed after the server receives the request, but before you will handle it.
So in your handling of the requests you need no rendering engine and so on -> make res.send(status, repsonseContent); and the server will send the response.
like this example:
start server:
var express = require('express'),
app = express();
app.listen(8000, function () {
console.log('NodeJS Server hört auf Port 8000');
});
Handle a simple post request:
app.post('/api/login', function (req, res) {
// req -> req object
// res -> res to finish request
// do something
res.send(200, {
message: 'loggedIn'
});
});
You should install additional packages for expressjs:
body-parser -> access request body simple over req.body.KEY
method-override -> now you can also send put and delete requests
Then you little server would looks like this:
var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
app = express();
app.use(bodyParser.json()); // handle json request data
app.use(bodyParser.urlencoded({
extended: true
})); // possibility to handle also form-param requests
app.use(methodOverride()); // HTTP PUT and DELETE support
app.listen(8000, function () {
console.log('NodeJS Server hört auf Port 8000');
});
For express there are 3 types of parameter:
-
url parameters from a url-scheme
like app.use(â/login/:typeâ, âŠ) params.type = the url parameter of the type
-
ordinary query parameters like 'http://âŠ/test?param1=xxxxx&value1=yyyyyâ
you can do req.query.param1 to get the values.
-
request-data like sending ajax request or json request
do req.body.KEY to get the value