Integrating Node.ACS with ExpressJS... not working

You must Login before you can answer or comment on any questions.

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!!!!!'
    });

— answered 8 months ago by Alan Wamser
answer permalink
2 Comments
  • this is helpful but it is a small piece... how do I include additional middleware? I mean there are an assortment of other issues around how do you structure a real application using the framework.

    I guess that there will be no official response since this question has not been answered by the ACS team i will figure it out and share with the others who are interested

    — commented 8 months ago by Aaron Saunders

  • I am also trying to see if it is possible to leverage full middleware power of express in node.acs. I created a simple login/register app fully in Node.ACS, please feel free to take a look in case if it be any use to you. It is checked-in to https://github.com/opolyo01/nodeACS and deployed to http://myrussiancup.com

    — commented 7 months ago by Oleg Polyakov

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.

— answered 8 months ago by Bill Wang
answer permalink
1 Comment
  • i understand that, I guess I am asking how does one integrate express with ACS?

    Is there a simple example... I think that this is something that a few people might want to do, either with expressjs or one of the many other frameworks people use on top of node

    — commented 8 months ago by Aaron Saunders

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.

— answered 8 months ago by Yuping Jin
answer permalink
3 Comments
  • Yuping, is there a place to find info on all the global objects and methods available to us in node.acs?

    — commented 8 months ago by Adam Paxton

  • Adam, as far as I can tell, you can use the following objects/functions for now. There are others which are still being working on. I think a list will be added to the wiki page soon.

    • console
    • logger
    • require
    • __dirname : directory where the app located
    • __filename : file name of the app's main executable
    • __name : app name
    • api
    • filter
    • ACS
    • setInterval
    • setTimeout

    Thanks!

    — commented 8 months ago by Yuping Jin

  • This seems like too much magic is going on. If ExpressJS is being used under the hood...how do we get access to the ExpressJS framework? There's a lot of goodness in there that people use in other NodeJS apps. They'll need to know how to map to it.

    — commented 8 months ago by Rick Blalock

Your Answer

Think you can help? Login to answer this question!