Search code examples
node.jsrestcreate.js

Creating REST API using nodejs for use with contentBlocks (createjs.org)


My question here is pretty specific but struggling with how to setup a REST find call to be used with the contentBlocks NodeJS plugin (https://github.com/primaryobjects/contentblocks).

My contentBlock paths are setup as follows:

var contentBlocks = require('contentblocks')(
{ 
    app: app, 
    host: 'localhost', 
    pathFind: '/content/find?q={"@subject":"[id]"}',
    pathPost: '/content', 
    pathPut: '/content/[id]', 
    pathDelete: '/content/[id]' 
});

My Route is setup as:

router.get('/content/find?q={"@subject":"[id]"}', content.find);

Which maps to content.js:

exports.find = function(req, res) {
    res.json("[]");
}

When I execute, I keep getting the following error when the page is accessed:

GET /content/find?q={"@subject":"<homePage_description>"} 404 275.193 ms - 3986

undefined:1
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/styles
^
SyntaxError: Unexpected token <
    at Object.parse (native)
    at c:\dev\camsc\node_modules\contentblocks\lib\managers\WebManager.js:19:42
    at IncomingMessage.<anonymous> (c:\dev\camsc\node_modules\contentblocks\node_modules\e
asypost\lib\easypost.js:19:13)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

And my view is as follows:

block content
    h1= title
    #homeContentBlock(about='homePage_description')
        div(property='content')
            p.
                Some text here...

I think the issues is from the '<' in the find request that is being passed, but this is part of how the contentBlocks package passes the data so not sure how to encode that data coming in.

Any help is greatly appreciated.


Solution

  • I figured out what I was doing wrong here. I had my route setup incorrectly:

    router.get('/content/find?q={"@subject":"[id]"}', content.find);
    

    Should be simply:

    router.get('/content/find', content.find);
    

    This now gets routed correctly.