Fast, lightweight, self contained, and easy to use web framework for nodejs.
Stir Fry is a framework for making web servers in nodejs. It enables you to quickly and easily create web apps and servers. So, here is how to create one:
The first step is to create a server program that uses Stir Fry. Start by creating a folder, you can call it anything you want.
Make sure you have Node.js installed on your computer. https://nodejs.org/en/download/
Next navigate to that folder in terminal and run this command:
npm install stirfry
That installs Stir Fry into the folder your server is running from. Next create a file called server.js
and open it with your favourite code editor. Add this code:
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.request(function (request, response) {
response.send("Hello World!");
});
To run server.js
, type:
node server.js
make sure you are in the same directory that has the server.js
file in the terminal
If that doesn't work, try:
nodejs server.js
If that doesn't work, you must install nodejs.
Assuming you've done it right, you can go to localhost:8080
in any web browser and it should show Hello World!
Setting the server to equal a new StirFry(8080)
meant that you were telling the server to listen for any request on port 8080. Then calling server.request
added the input as a response for requests.
After all of the listeners have been called it sends the response to the user. You can add to the response by writing
response.send("The thing you want to add to the response");
So by typing response.send("Hello World!")
you made that the response.
Stir Fry gives you the ability to preprocess the request and response objects. Basically that means you can write exensions and mods for stirfry. Here is an example
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.pre(function(request, response) {
//Now I can change the request and response object before the next code runs
request.doubleURL = request.url + request.url;
}
server.request(function(request, response) {
//Now I can access request.doubleURL, and I also can in every request listener
respose.send(request.doubleURL);
});
You can create extensions using basically the same syntax as a normal server and use them just by saying server.use(extension)
, here is an example
Keep in mind, extensions do not support adding layers. If you wish to create layers in your extension create a function that takes the server as an input and call it.
"use strict";
let StirFry = require('stirfry');
let extension = new StirFry.extension();
//I can put more preprocessors and responders if I want
extension.pre(function(request, response) {
request.doubleURL = request.url + request.url;
});
let server = new StirFry(8080);
server.use(extension);
server.request(function(request, response) {
//I can use request.doubleURL
response.send(request.doubleURL);
});
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.request(function (request, response) {
response.send("Hello World!");
});
This example creates a server on port 8080 and sets it to respond with Hello World!
on any request. When you use response.send
it appends the input to the response.
Stir Fry has a static file server method built in. All you need to do is this:
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.request(StirFry.static('public'));
Public is the folder that the files get served from.
Stir Fry lets you run multiple asynchronous operations at once. You can do all the preprocessing you want in the server.process
layer, and then once all of those are done, it runs server.pre
listeners, and once those are done it runs server.request
listeners.
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
let fs = require('fs');
server.pre(function (request, response, end, async) {
async.start();
fs.readFile('file.txt', function (err, data) {
response.data = data.toString();
async.done();
});
});
server.request(function (request, response) {
response.send(response.data);
});
This program uses fs.readFile
to add a property to the response object and then sends it to the user. There are loads of more efficient ways to do this, this is just an example of how to use async.
Stir Fry has a built in response.sendFile
method, here is an example:
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.request(function (request, response) {
response.sendFile('file.html');
});
When you create a request, preprocessor, or processor listener, you have the option of limiting it to certain requests by regex matching. Here is an example:
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.request(/\/.*?\/hi/, function (request, response) {
response.send("hi");
});
You can access regex capture groups by accessing request.params
as an array. request.params
also processes query strings in the request.
Just write server.use(thePluginObject)
Creating plugins works in a very similar way as creating servers. The only difference is you use new StirFry.extension()
instead of new StirFry()
. Then you can say server.use(extension)
and it manages all of the listeners. Here is an example
"use strict";
let StirFry = require('stirfry');
let extension = new StirFry.extension();
extension.req(function(request, response) {
let log = `Request recieved with ${request.post ? `${request.post} as post and `:``} ${request.fullUrl || request.url} as the url. Recieved from ${request.ip} on `+ formatDate(new Date()); //Format date is defined externally
console.log(log);
});
That is similar to the built in logger extension. Here is how you can use it
"use strict";
let server = new StirFry(8080);
server.use(extension);
The built in logger is a function that returns an extension because people are able to define a log file
"use strict";
let StirFry = require('stirfry');
let server = new StirFry(8080);
server.use(StirFry.logger("logFile"));
You can access post data by accessing request.post
as an associative array
As of 1.6.0 StirFry allows the creation of custom layers in the server. The syntax for this involves the three functions server.createLayer
, server.destroyLayer
, and server.placeLayer
.
Create layer takes a string that is the name of the layer to create.
Destroy layer is the same as create layer but it will remove the it instead of creating it.
Place layer will take the names of two layers it will make it so that the layer that has the same name as the first input, always gets called after the layer that has the name of the second input.
"use strict";
let StirFry = require('../../stirfry.js');
let server = new StirFry(8080, '0.0.0.0');
server.createLayer('final');
server.placeLayer('final', 'request');
server.addListenerOnLayer('final', function(req, res) {
res.send('Hello World!');
});
The original author of Stir Fry is Alex Waese-Perlman