Skip to content

Latest commit

 

History

History

server

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

server module


Access

gracenode.server

Configurations

"modules": {
	"server": {
		"port": port number,
		"host": host name or IP address,
		"controllerPath": path to controller directory,
		"ignored": ['name of ignored URI'...],
		"error": {
			"404": {
				"controller": controller name,
				"method": public controller method
			},
			"500": ...
		},
		"reroute": [
			{
				"from": '/',
				"to": 'another/place'
			},
			...
		]
	}
}

######API: start

void start()

Starts an HTTP or HTTPS server

#####API: userError

void userError(mixed error, mixed response, Function callback)

Responds to the client with status code 404

#####API: error

void error(mixed error, mixed response, Function callback)

Responds to the client with status code 500

Example:

Example of how to set up a server

// index.js file of an application
var gracenode = require('./GraceNode/');
gracenode.use('server', 'server');
gracenode.setup(function (error) {
	if (error) {
		throw new Error('failed to set up GraceNode');
	}
	// we start the server as soon as GraceNode is ready
	gracenode.server.start();
});

Controller

// controller/example/index.js > /example/foo/
var gracenode = require('../GraceNode/');
// this will become part of the URI
// the first argument is **ALWAYS** requestObject
module.exports.foo = function (requestObject, serverCallback) {
	// serverCallback is created by server module automatically
	cb(null. 'foo', 'JSON');
};
// /example/foo/ will display "foo" on your browser

How to read GET and POST

// controller file
module.exrpots.index = function (requestObject, cb) {
	// server module automatically gives every controller the following functions:
	// requestObject.getData and requestObject.postData
	var getFoo = requestObject.getData.get('foo');
	var postFoo = requestObject.postData.get('foot');
	cb(null, null, 'JSON');
};

How to read request headers

// controller file
module.exports.index = function (requestObject, cb) {
	// server module automatically gives every contrller the following function:
	// requestObject.requestHeaders > an instance of Headers class
	var os = requestObject.requestHeaders.getOs();
};

Headers class

get

String get(String headerName)

getOs

String getOs()

getBrowser

String getBrowser()

getDefaultLang

String getDefaultLang

How to set response headers

// controller
module.exports.index = function (requestObject, cb) {
	// server module automatically gives every contrller the following function:
	// requestObject.setHeader
	module.exports.setHeader('myHeader', 'foo');
};

How to read and set cookie

// controller
module.exports.index = function (requestObject, cb) {
	// server module automatically gives every contrller the following functions:
	// requestObject.getCookie and module.exports.setCookie
	var sessionCookie = requestObject.getCookie('session');
	requestObject.setCookie('myCookie', 'foo');
	// for handling session please use session module
};

How to handle and pass parameters

// controller
// request URI /foo/index/one/two/
module.exports.index = function (requestObject, cb) {
	var parameters = requestObject.parameters;
	// parameters: ["one", "two"]
};