![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
loco-server
Advanced tools
Loco is a http proxy and/or mockup server.
npm install -sD loco-server
Straight from the command line:
npx loco <config_file>
or if used in package.json scripts:
loco <config_file>
Sample config file:
{
"appPort": 8888,
"appHost": "127.0.0.1",
"functionsPath": ["loco_functions/*.js"],
"envFile": ".env",
"reloadOnRequest": true,
"optionsRequestHeaders": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
},
"optionsRequestStatusCode": 200
}
Field | Description |
---|---|
appPort | Port to run the server on |
appHost | Host to run the server on |
functionsPath | Glob to where to look for loco functions |
envFile | .env file name with path |
reloadOnRequest | Flag should funtions be reload on each request which will function as live-reload |
optionsRequestHeaders | Response headers for OPTIONS request |
optionsRequestStatusCode | OPTIONS request status code |
Functions are understood as JS files specified by functionsPath in the configuration. Each file will be served under a separate webpath in the server. This webpath is equal to the file namie without .js extension.
Function file scaffold:
/**
* Function description string
*/
const description = 'Blank function scaffold.';
/**
* Main process function
* @param {object} param.req entire request object
* @param {object} param.query GET request query
* @param {object} param.bodyJSON POST request body
* @param {string} param.reqMethod request method
* @param {object} param.loco helper functions object
* @param {object} param.envVars environment variables
* @param {array} param.paths array of wildcard paths
* @returns {object} response object consisting of response statusCode, body and headers object
*/
const processFunction = async (param) => {
return {
statusCode: 200,
headers: param.loco.corsHeaders(),
body: {mockup: true}
};
};
// Export
module.exports = {
processFunction,
description,
};
The descriptions const is used for providing a short summary of a given function. Main required function in each function file is the processFunction. It has one parameter called param which is an object containing:
Argument | Descriptions |
---|---|
req | full express request object |
query | GET query requested represented as JSON, example: {"query":"test"} |
bodyJSON | POST body requested as as JSON, example: {"login":"john"} |
reqMethod | Request method as a string, example: GET |
loco | object containing predefined helper function, described below in the function helpers section |
envVars | environment variables from the .env file defined in the config |
paths | array of paths that were used when this is a wildcard request |
The function must return an object with a given structure:
{
statusCode: 200,
headers: param.loco.corsHeaders(),
body: {mockup: true}
}
Key | Value |
---|---|
statusCode | response status code |
headers | object containig response header as key and header value as value |
body | response body |
Current list of helpers passed as a param.loco argument to the processFunction
param.loco.fetchJSON(url:string, options:object)
- node-fetch moduleparam.loco.returnJsonWithDelay(sec, jsonResponse)
- usefull when a mocked response is return to fake a time delay. Example usage:return {
[...]
body: await loco.returnJsonWithDelay(3, jsonData),
};
this will return jsonData after 3 seconds.
loco.corsHeaders(headersOverwrites)
- returns headers that can be used when you want to enable cors:{
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-type',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
}
Example usage:
return {
[...]
headers: param.loco.corsHeaders(),
[...]
};
You can also overwrite a default header or add a new one:
headers: param.loco.corsHeaders({"Content-Type": "text/html; charset=utf-8"}),
loco.fetchJSON(url:string, options:object)
- utility function to make a fetch request to an endpoint that returns JSON response. Example usage:const jsonData = await param.loco.fetchJSON('https://api.url/', {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(param.bodyJSON.payload)
}).catch((err) => {
return {error: err.message};
});
[...]
return {
[...]
body: jsonData,
};
param.loco.requireUncached(path)
- clear cache for path and then require. Usefull when you modify data in processFunction often.If you find this piece of code to be useful, please consider a donation :)
FAQs
HTTP Proxy and/or mockup server
The npm package loco-server receives a total of 23 weekly downloads. As such, loco-server popularity was classified as not popular.
We found that loco-server demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.