Skip to content

Instantly share code, notes, and snippets.

@wess
Last active December 20, 2015 04:59
Show Gist options
  • Save wess/6074779 to your computer and use it in GitHub Desktop.
Save wess/6074779 to your computer and use it in GitHub Desktop.
A quick Node script to stub out RESTful webservices
// The MIT License (MIT)
//
// Copyright (c) 2013 Wess Cope
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// Restful Folder.
//
// Created by Wess Cope on 2013-07-24.
// Copyright 2013 Wess Cope. All rights reserved.
//
/*
RESTFul Folder is a simple and quick script to stub out RESTful webservices. It simply
takes whatever folder you run it in and converts it to a rest service.
Setup: Create a root folder and app.js. Put this script in app.js.
Any subfolders you create become endpoints to your requests. In those folders
create a file per request method. So if you create a "post.json" file, any posts
requests made to the server to that files directory, will return the contents of post.json.
Yeah, that simple.
Example:
MyService/
- app.js
- user/
- get.json
- create/
- post.json
Stub URLS:
GET: localhost:8080/user/
POST: localhost:8080/user/create/
*/
var http = require('http'),
port = 8080,
host = "0.0.0.0";
http.createServer(function(request, response){
var url = require('url').parse(request.url).pathname,
path = require('path'),
fs = require('fs'),
filename = path.join(process.cwd(), url),
method = request.method;
console.dir(method.toUpperCase() + ": " + url);
fs.exists(filename, function(exists){
if(!exists)
{
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if(fs.statSync(filename).isDirectory())
filename += '/' + method;
fs.readFile(filename + ".json", function(error, data) {
if(error)
{
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
}
response.writeHead(200, {"Content-Type": "application/json"});
response.write(data + "\n");
response.end();
return;
});
});
}).listen(port, host);
console.log("Server running at: " + host + ":" + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment