Completely impractical server-side JS/Node.js examples


#1

using express, because one http server per process isn’t enough

mkdir -p resources/[local]/weeb/
cd resources/[local]/weeb/
npm install express

weeb/__resource.lua:

server_script 'index.js'

weeb/index.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
	setImmediate(() => {
		const numIndices = GetNumPlayerIndices();
		
		const playerArray = [];
		
		for (let pidx = 0; pidx < numIndices; pidx++) {
			const player = GetPlayerFromIndex(pidx);
			
			const identifiers = [];
			const numIdentifiers = GetNumPlayerIdentifiers(player);
			
			for (let id = 0; id < numIdentifiers; id++) {
				identifiers.push(GetPlayerIdentifier(player, id));
			}
			
			playerArray.push({
				id: player | 0,
				guid: GetPlayerGuid(player),
				identifiers,
				name: GetPlayerName(player),
				ping: GetPlayerPing(player),
				endpoint: GetPlayerEndpoint(player)
			});
		}
		
		res.setHeader('content-type', 'application/json');
		res.send(JSON.stringify(playerArray));
	});
});

app.get('/chat', (req, res) => {
	setImmediate(() => {
		// no TriggerClientEvent wrapper yet :(
		emit('_chat:messageEntered', 'meow?', [0, 255, 0], req.query.message);
	});
	
	res.send('ok');
});

app.listen(3000, () => console.log('Example app listening on port 3000!'));

webpack on the server, and some other example

mkdir -p resources/[local]/what/
cd resources/[local]/what/
npm install webpack
npm install color

what/__resource.lua:

server_script 'amazing.js'

what/amazing.js:

console.log(process.version);

RegisterCommand('http_get', (source, args) =>
{
	const http = require('http');

	http.get(args[0], (res) =>
	{
		res.setEncoding('utf8');
		let rawData = '';
		res.on('data', (chunk) => { rawData += chunk; });
		res.on('end', () => {
			try {
				setImmediate(() => {
					SaveResourceFile(GetCurrentResourceName(), 'test.json', rawData, -1);
				});
				console.log(rawData);
			} catch (e) {
				console.error(e.message);
			}
		});

	});
});

const path = require('path');
const weebpack = require('webpack');

const root = GetResourcePath(GetCurrentResourceName());

weebpack({
	entry: root + '/weeb/lol.js',
	output: {
		filename: 'meow.js',
		path: path.resolve(root + '/weebs')
	},
}, (err, stats) => {
	console.log('webpack built!');

	if (err) {
		console.error(err.stack || err);
		if (err.details) {
			console.error(err.details);
		}
		return;
	}

	const info = stats.toJson();

	if (stats.hasErrors()) {
		console.error(info.errors);
	}

	if (stats.hasWarnings()) {
		console.warn(info.warnings);
	}
});

what/weeb/lol.js:

// hmm
const Color = require('color');

// test
const color = Color.rgb(255, 0, 255);
console.log('color? ' + color.hsl().string());

console example:

http_get "http://localhost:30120/info.json"

#2

Noice :grinning:


#3

:grin:

(20 characters)


#4

:heart_eyes: :smile:


#5

:heart: :heart: :heart:


#6

screenshot?

20 characters…


#7

This is really nice. I’ll test it immediately!


#8

how do you think we could do a TriggerEvent / TriggerServerEvent system between the LUA code and the JS code?