Local Rest API with NodeJS an SQLite


#1

Hi,
I installed NodeJS and used the following tutorial (https://github.com/romwaldtff/NodeJS-REST-API-SQLite) to set up a local REST API. It uses an SQLite Database which I filled with some demo data. It works fine with the REST node. The problem is, that it works only inside Noodl, but it doesn’t work inside a browser (http://localhost:8574/external/viewer/index.html) or on a mobile phone. Is this a bug or what am I missing? Please help me. If I got this running, everyone could build prototypes with simple access to a relational database (SQLite).

Details to my example:
Rest Node:
Endpoint: http://localhost:3000
Resrouce: /api/car
Method: GET
Script:
define({
// The input ports of the REST node, name of input and type
inputs:{
//ExampleInput:‘number’,
},

// The output ports of the REST node, name of output and type
outputs:{
    ExampleOutput:'string'
},

request:function(inputs,request) {
},

response:function(outputs,response) {
    
    console.log("ResponseFunction");
    
    var colors = response.content; 
    var color = colors[1];
    
    console.log(colors.length);
    console.log(color.model);
    
    outputs.ExampleOutput = color.model;
}

})

Thanks for your help, Philipp


#2

This is probably due to CORS problems. If you check the developer console, and it says something along the lines of “No Access Control Allow Origin Header on the requested resource”, it means you need to update your Node.js server to use CORS headers.

Basically, for security reasons, browsers refuse to connect to a server if the server doesn’t specifically allow browsers to do that through a few special headers. Noodl is a native app and therefore works fine, but a browser will refuse to connect.

Fortunately it’s an easy fix. Add this middleware to your express server and it should work: https://github.com/expressjs/cors. Follow the Simple Usage (Enable All CORS Requests) example, no need for any additional configuration.

Hope that helps!


#3

Hello micart, it works. Thank you very much :slight_smile: