Looking for a simple example of integrated node.acs with expressjs?
I have a bunch of stuff already written in ExpressJS and I would like to try and move it all over
So this is my original code... just a snippet that causes crashes... tried to change app to api but still failure
/** * Module dependencies. */ var express = require('express'); var app = express(); var querystring = require('querystring'); // config app.set('view engine', 'ejs'); app.set('views', __dirname + '/views'); // middleware app.use(express.bodyParser()); app.use(express.cookieParser('shhhh, very secret')); app.use(express.session());and then I make calls like this
app.get('/', function(req, res) { res.redirect('login'); });
3 Answers
It looks like node.acs is configured to use Express with EJS. I created a view and added the following template.
<html> <head> <title><%= message %></title> </head> <body> <p> <%= message %> </p> </body> </html>Then updated my my node app to use the render vs. the text in the request.
res.render('index', { message : 'Hello, world!!!!!' });
The "api" in node.acs project is not the "app" object in expressjs, when you define a api.xxx function in the project, means you define a function that handles "/xxx" path, the "req" and "res" passed in are the expressjs request and response objects. If you are trying to do redirect, the path need to be start with "/", i just tried without "/", it doesn't work. Also, whatever the redirect path is, it has to be defined in the project as well to handle the redirect request.
Node.ACS does support filters for middlewares. Please try the following code.
/** * service definition file */ logger.setLevel('DEBUG'); var response = ''; api.index = function(req, res) { res.text(response + 'Hello, world!'); logger.info('This is an info message. ' + new Date()); logger.error('This is an error message. ' + new Date()); logger.warn('This is a warning message. ' + new Date()); }; filter.f1 = function(req, res, next) { response += 'filter1 done\n'; next(); }; filter.f2 = function(req, res, next) { response += 'filter2 done\n'; next(); };Just add your filters to the 'filter' object.
Node.ACS doesn't expect developers to define routing (app.get('/',...) in app code. Instead, app code is running in a built-in web application which acts as a container. Routing stuff has been done by the container. So developers just need to add methods to the 'api' object and the container is responsible for routing requests to the methods.
Your Answer
Think you can help? Login to answer this question!