{"id":1693,"date":"2012-07-31T07:29:50","date_gmt":"2012-07-31T12:29:50","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1693"},"modified":"2012-07-31T07:33:49","modified_gmt":"2012-07-31T12:33:49","slug":"simple-node-based-http-put-simulator","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1693","title":{"rendered":"Simple Node based Http Put simulator"},"content":{"rendered":"

It\u2019s great what you can accomplish in a few lines of code. The Node<\/a> based source code below uses express<\/a> to create a mini view-based web server along with a mock Http Put file upload destination. <\/p>\n

\n

\/**<\/span>
* Module dependencies.<\/span>
*\/<\/span>

var express = require('express'<\/span>)
, routes = require('.\/routes'<\/span>)
, http = require('http'<\/span>)
, path = require('path'<\/span>);

var app = express();

app.configure(function(){
app.set('port'<\/span>, process.env.PORT || 8080);
app.set('views'<\/span>, __dirname + '\/views'<\/span>);
app.set('view engine'<\/span>, 'jade'<\/span>);
app.use(express.favicon());
app.use(express.logger('dev'<\/span>));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static<\/span>(path.join(__dirname, 'public'<\/span>)));
});

app.put('\/upload\/:fileUpload'<\/span>, function(req, res, next){
console.log('uploading!'<\/span>);
res.send('OK'<\/span>, { 'Content-Type'<\/span>: 'text\/plain'<\/span> }, 200);
});

app.configure('development'<\/span>, function(){
app.use(express.errorHandler());
});

app.get('\/'<\/span>, routes.index);

http.createServer(app).listen(app.get('port'<\/span>), function(){
console.log("Express server listening on port "<\/span> + app.get('port'<\/span>));
});
<\/pre>\n

<\/div>\n

Using WebStorm<\/a>\u2019s Node\/Express template, I only added 4 lines:<\/p>\n

\n
app.put('\/upload\/:fileUpload'<\/span>, function(req, res, next){
console.log('uploading!'<\/span>);
res.send('OK'<\/span>, { 'Content-Type'<\/span>: 'text\/plain'<\/span> }, 200);
});
<\/pre>\n

<\/div>\n

These 4 lines added a new route\/path for uploading a file in the pattern of \/upload\/ {fileName}.<\/p>\n

The response was \u201cOK.\u201d <\/p>\n

I\u2019d tried this same thing using ASP.NET MVC 4 (with Razor), but was stumped by the error when trying to use HttpPut on an Action in the controller.<\/p>\n

I was using this end-point as a mock-upload destination for my SmugMug application, SnugUp<\/a>. I wanted to eliminate the uploading process as it\u2019s time consuming and messes up my SmugMug galleries! \"Smile\"<\/p>\n

The C# code is simple as it uses HttpWebRequest:<\/p>\n

\n
HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(ApplicationConstants.UploadUrl + "photo.jpg"<\/span>);

uploadRequest.Timeout = (int<\/span>)10080 * 60 * 1000; \/\/ 7 days <\/span>
uploadRequest.Method = "PUT"<\/span>;
uploadRequest.UserAgent = ApplicationConstants.UserAgent;
uploadRequest.ContentLength = fi.Length;
uploadRequest.KeepAlive = true<\/span>;<\/pre>\n

<\/div>\n

(SmugMug grabs the file name from a custom Http Header, so, the put URL is always \u201cphoto.jpg\u201d.)<\/p>\n","protected":false},"excerpt":{"rendered":"

It\u2019s great what you can accomplish in a few lines of code. The Node based source code below uses express to create a mini view-based web server along with a mock Http Put file upload destination. \/** * Module dependencies. *\/var express = require(‘express’) , routes = require(‘.\/routes’) , http = require(‘http’) , path = […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4],"tags":[82,103,104,102],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-rj","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1930,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1930","url_meta":{"origin":1693,"position":0},"title":"Sending a socket to a forked process in Node.JS","date":"August 27, 2013","format":false,"excerpt":"If you want to fork a process in Node and pass a socket, the current Node documentation has a rather odd example, so I\u2019ve simplified it here for my own sanity (further complicated by the fact that the WebStorm debugger can\u2019t debug a forked Node.JS process, which confused me for\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1820,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1820","url_meta":{"origin":1693,"position":1},"title":"Knockout binding for JavaScript route fixup","date":"January 25, 2013","format":false,"excerpt":"Part one. After the first round, I felt compelled to KnockOut the code a bit more. I\u2019d mentioned I wasn\u2019t pleased with the code exactly. It needed some refactoring. So, I\u2019ve created a new Knockout binding handler. This binding handler replaces\u00a0 named parameters with a model\u2019s properties in a path.\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1974,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1974","url_meta":{"origin":1693,"position":2},"title":"Loading Models in NodeJS","date":"December 2, 2013","format":false,"excerpt":"I\u2019d answered a question on StackOverflow about where to put \u201cmodels\u201d in a NodeJS project. I wanted to elaborate on the simple auto loader I use to load a folder full of models (and I use this pattern other places as well). Normally, I create a folder called models: Inside\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1754,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1754","url_meta":{"origin":1693,"position":3},"title":"Nest Thermostat API using Node JS and Nest API Update","date":"October 9, 2012","format":false,"excerpt":"I\u2019ve been asked by a few people for more details on the API Nest Labs uses for their thermostats, especially regarding setting data (and not just polling). The API uses mostly JSON formatted data POSTed to their web servers. Authentication To authenticate, POST the username and password, encoded as form\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1800,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1800","url_meta":{"origin":1693,"position":4},"title":"Named routing with Knockout, ASP.NET MVC, and AttributeRouting, from JavaScript","date":"January 24, 2013","format":false,"excerpt":"It's hard to describe exactly what I've built here, but I'm just throwing these pieces out on the Internet in case someone: a) finds them useful, or b) has a better solution. Goals: Named routes, with a wee bit of a Rails feel A Knockout friendly syntax for binding to\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1570,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1570","url_meta":{"origin":1693,"position":5},"title":"JavaScript: isScrolledIntoView","date":"March 15, 2012","format":false,"excerpt":"I needed a simple way to detect when a placeholder DIV (that would contain an image) had scrolled into the current viewport of the browser. I\u2019ve seen a few solutions that worked, and a few that didn\u2019t (hello? test your code!). Here\u2019s my simple solution: function isScrolledIntoView(elem) { elem =\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"SmugMupBrowser-live","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/03\/SmugMupBrowser-live.gif?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1693"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1693"}],"version-history":[{"count":2,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1693\/revisions"}],"predecessor-version":[{"id":1695,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1693\/revisions\/1695"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}