> For the complete documentation index, see [llms.txt](https://backbench.gitbook.io/backbench-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://backbench.gitbook.io/backbench-docs/getting-started/nodejs.md).

# Node

We have a default Node **6.10**(LTS) support on our platform. Custom version support also available on request.

Since backbench is a Faas integrated with APIGATEWAY we provide a static convention to map your functions to API endpoints. (we call em *public functions*). And this is how we define them -  &#x20;

```javascript
exports.endpoint = function(arg1, arg2){
    //your code here
}
```

`arg1` is the **request object** for node. This containers information about the HTTP state. Goes as;

```
{  
    "method": "",  
    "path": "",  
    "body": {},  
    "headers": {},
    "query": {} 
}
```

`arg2` is a **nodejs callback** and is optional. Callback will let you see the response on your APIs. (not console.log)

**Note**: callback function should have an argument which is `JSON.parsed`.&#x20;

* To log your application, use console.log() and logs will be generated on **Logs section**.&#x20;
* To check compilation error, click on bug button(integrated JSHint).&#x20;
* For runtime failure, see your bench's dashboard.&#x20;

## Restrictions

* Module name cannot be `func.js`.
* Callback function's argument should be `JSON.parsed()` to use.

## Example

1. API to display Hello World!

```javascript
exports.endpoint = function(request, callback){
    console.log("Logged at " + new Date()) 
    callback(undefined,  {response: "Hello World"})
}
```

&#x20;  2\. Running a background process (without callback).&#x20;

```javascript
exports.endpoint = function(request){
    //this program can be used to log IP address, 
    //for the user hitting this api.
    console.log(req["x-forwarded-for"]);
}
```

&#x20;  3\. Using ESv6

```javascript
exports.endpoint = (req, cb) =>{
    cb(null, {message: "Hello World"})
}
```

&#x20; 4\. Using node `async/await`.&#x20;

```javascript
var request = require("request");
exports.endpoint = async function(req, cb){
    var body = await request.get('https://api.github.com/repos/peeyushsrj'); 
    cb(undefined, body)
}
```

## More about callback function

```javascript
callback(Error error, Object result);
```

* `error` – is an optional parameter that you can use to provide results of the failed function execution. When a function succeeds, you can pass `null` as the first parameter.
* `result` – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be `JSON.parse()` compatible. If an error is provided, this parameter is ignored.
