Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active April 1, 2019 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/b49ce1e100aa811993b6ab112a65e6b0 to your computer and use it in GitHub Desktop.
Save andersevenrud/b49ce1e100aa811993b6ab112a65e6b0 to your computer and use it in GitHub Desktop.
Simple OS.js Application w/Server API
//
// Just a standard OS.js application
//
// The "index.js" file.
//
import osjs from 'osjs';
import {name as applicationName} from './metadata.json';
const register = (core, args, options, metadata) => {
const proc = core.make('osjs/application', {args, options, metadata});
proc.createWindow({
id: 'ApiExampleWindow',
title: metadata.title.en_EN,
dimension: {width: 400, height: 400},
position: 'center'
})
.on('destroy', () => proc.destroy())
.render($content => {
// Just a simple function that sets text into the window
const setWindowContents = data => {
$content.appendChild(
document.createTextNode(JSON.stringify(data))
);
};
// Perform a request to our application server API
// See 'server.js'
proc
.request('/my-server-method', {
method: 'post',
body: {
title: 'This is just a test',
body: 'My article'
}
})
.then(result => {
// Just dump the result into our window
setWindowContents(result);
})
.catch(error => {
// Just dump any error into our window
setWindowContents(error.toString());
})
});
return proc;
};
// Creates the internal callback function when OS.js launches an application
osjs.register(applicationName, register);
//
// Just a standard OS.js application server
//
// This is the "server.js" file that lies in your application.
//
// A couple of notes:
// 1. If you don't use nodemon to automatically restart the server when
// files are changed, you have to restart your server *manually*
// after changing this file.
// 2. This file needs to be defined as `{"server": "server.js"}` in
// metadata.json, but it is by default.
// 3. This file has been shortened a bit to remove the noise.
// Your file might differ. You only need to worry about this init method.
//
const request = require('request'); // npm install request
module.exports = (core, proc) => ({
// Creates routes on the server when it starts
init: async () => {
// This is the endpoint we call in our client
core.app.post(proc.resource('/my-server-method'), (req, res) => {
// Creates a request to the external API
const url = 'https://jsonplaceholder.typicode.com/posts';
const body = req.body; // This is our JSON from client
// This is an example how to use the request library to perform
// a JSON POST operation. This can be generalized
request({
url,
body,
method: 'post',
json: true
}, (error, response, body) => {
if (error) {
res.status(500)
.json({error: error.toString()});
} else {
res
.status(response.statusCode)
.json(body);
}
})
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment