public class WebApiApplication : System.Web.HttpApplication { protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0. HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies. HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0. HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies. if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition, Content-Description"); HttpContext.Current.Response.End(); } } }
martes, 28 de enero de 2014
Enable CORS in ASP.NET WebAPI
In Global.asax.cs file
jueves, 23 de enero de 2014
martes, 7 de enero de 2014
Start with nodejs server
var http = require('http'); var mime = require('mime'); var url = require('url'); var fs = require('fs'); var _base = './'; http.createServer(function(req, res){ pathname = _base + req.url; console.log('path name: ' + pathname); fs.stat(pathname, function(err, stats){ if(err){ res.writeHead(404); res.write('Bad request 404 \n'); res.end(); }else if(stats.isFile()){ var type = mime.lookup(pathname); console.log('mime-type: ' + type); res.setHeader('Content-Type', type); res.statusCode = 200; var file = fs.createReadStream(pathname); file.on('open', function(data){ file.pipe(res); }); file.on('error', function(err){ console.log('error open the file: ' + err); }) }else{ res.writeHead(403); res.write('Directory access is forbidden'); res.end(); } }); }).listen(1024); console.log('server running on 1024');
Playing with MongoDB & nodejs
I´m reading learning nodejs book and this is a little example. It´s not the best solution, but it´s a piece of code.
var mongodb = require('mongodb'); var bugRepository = module.exports = {}; var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true}); bugRepository.database = new mongodb.Db('bugdb', server); var self = bugRepository; bugRepository.insert = function(bug){ console.log('insert ' + bug); self.database.collection('bugs', function(err, collection){ collection.insert([bug], {safe:true}, function(err, result){ if(err){ console.log('err saving new bug: ' + err); return; } if(result){ console.log('result of saving bug: ' + result); } }); }); } bugRepository.delete = function(bug){ console.log('delete id ' + bug.idBug); self.database.collection('bugs', function(err, collection){ collection.remove({id: bug.idBug}, function(err, result){ if(err){ console.log('error removing one bug: ' + err); return; } if(result){ console.log('deleting result: ' + result); } return result; }); }); } bugRepository.get = function(bug){ console.log('get id: ' + bug.idBug); self.database.collection('bugs', function(err, collection){ collection.remove({id: bug.idBug}, function(err, doc){ if(err){ console.log('error removing one bug: ' + err); return; } if(doc){ console.log('getting a bug' + doc); return doc; } }); }); }With the test:
var repository = require('./bugRepository'); var testCase = require('nodeunit').testCase; module.exports = testCase({ //setup: function(){}, //teardown: function(){}, 'Save simple bug': function(test){ var bug = {}; bug.idBug = 1; bug.name = 'bug name'; bug.description = 'bug description'; repository.insert(bug); var expected = repository.get(bug); test.equal(bug, expected); repository.delete(bug); test.done(); } });for running the test:
nodejs /usr/local/bin/node_modules/nodeunit/bin/nodeunit bugRepository.test.js
Suscribirse a:
Entradas (Atom)