0.1.2 • Published 5 years ago

urlserver v0.1.2

Weekly downloads
12
License
See LICENSE.TXT
Repository
-
Last release
5 years ago

Url Server

By Tgwizman

I made this library to make it easier on myself to prototype web servers. I ended up writting a framework around this, so this is slowly getting pushed into a solid "product". This is what I have to offer.

Simple Usage

Hosting a server using HTTP

var ur = require('urlserver');

ur.use('/url', function(input, output) {
	output.type = 'text/html';
	output.data = 'Hello world!';
});

ur.server().listen(80);

Hosting a server using HTTPS

var ur = require('urlserver');

ur.use('/url', function(input, output) {
	output.type = 'text/html';
	output.data = 'Hello world!';
});

ur.server({
	key: ur.getFile(keyPath),
	cert: ur.getFile(certPath)
}).listen({port: 443});

Some Uses

Params, Post

Url parameters are separated from the url path and put into the input. Http post requests are waited on to get post body parameter string key value pairs and are also put into the input.

ur.use('/mypage', function(input, output) {
	var data = 'Actions:';

	if (input.params.action == 'myAction1') {
		console.log('param: myAction1');
		data = ' myAction1';
	}

	if (input.post.action == 'myAction2') {
		console.log('post body ["action"] = myAction2');
		data += ' myAction2';
	}

	output.type = 'text/plain';
	output.data = data;
});

Redirecting

If you need to redirect the user to a different page, it is so simple.

ur.use('/', function(input, output) {
	output.url = '/home';
});

File Receiving

Receiving a file is super simple. Just make an on load listener, and get the data when it's ready.

ur.use('/mypage', function(input, output) {
	input.onFilesLoad = function(files) {
		var file = files.uploadedFile;
		ur.log(`Saved file "${file.name}" at "${file.path}"`);
	};
});

Data Storage

Use files as data storages, and forget the hard parts. Load a database by path, and if it doesn't exist, it will be automatically created and loaded. When you save a database, you just reference it again by the path, and all the data will be saved.

var users = ur.getDB('db/users.json');

users['Tgwizman'] = {
	favoriteColor: '#369C'
};

ur.saveDB('db/users.json');

File 'db/users.json':

{
	"Tgwizman": {
		"favoriteColor": "#369C"
	}
}

Other Uses

Logging

Keeping track of times and dates can be very important!

ur.log('Blah. Some text.');
// output:
// '[06/13/2019 02:31:04] Blah. Some text.'

Files

Accessing files can be a nuisance. These methods should make everything pretty straight forward.

// get file
// returns file text
var str = ur.getFile('path/to/file.txt');

// get file raw
// returns file without forcing utf8
var file = ur.getFileRaw('path/to/file.txt');

// set file
// sets file text
ur.setFile('path/to/file.txt', 'file\n\tdata\nhere');

// get JSON file
// returns json object from file text
var obj = ur.getJSONFile('path/to/file.json');

// set JSON file
// saves json object as text to file
ur.setJSONFile('path/to/file.json');

// deletes file
ur.delFile('path/to/file.txt');

// deletes folder
// [WARN!] will recursively delete contents by default without warning
ur.delFolder('path/to/dir/');

// rename item
ur.rename('path/to/file.txt', 'path/to/new_file_name.txt');

// make folder
// parent folders must already exist
ur.makeDir('path/to/dir');

// check if file exists
var bool = ur.exists('path/to/file.txt');

// get the mime type of a file
var str = ur.getMime('path/to/file.txt');
// returns 'text/plain'

// check if file name is windows friendly
var bool = ur.fileNameIsWindowsFriendly('file.name%20(10_10-10).extension');

Miscellaneous

Anything that doesn't fall into one of the categories above is going to be tossed here for now.

// Get network addresses
var network = ur.getNetwork();
{
	"hostname": "MyComputerName",
	"networkInterfaces": {
		"wifi": [{
			"mac": "4567:89AB:CDEF",
			"addresses": [
				"192.168.0.5"
			]
		}],
		"loopback": [{
			"mac": "0123:4567:89AB",
			"addresses": [
				"127.0.0.1"
			]
		}]
	}
}

// Get date and time with format
var date_str = ur.getDate(), // '06/13/2019'
	time_str = ur.getTime(); // '02:31:04'

function callback(data)
// HTTP GET
ur.httpGet(url, options, callback)
// HTTPS GET
ur.httpsGet(url, options, callback)
// HTTP PUT
ur.httpPut(options, body, callback)

Requests

If you would like to see anything added or anything changed, contact me.

Errors

I have made this for myself. If you are going to be using this, please know that there are many bugs and errors will happen. If you come across any, please let me know and I will try to fix them as soon as I can.

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago