Permalink
Browse files

add structure for web service

  • Loading branch information...
1 parent b0afb72 commit 67c9f1a4dcf7b8dfe152b4e65db31d38d0dd0e8e @marty-wang committed Nov 3, 2011
Showing with 63 additions and 7 deletions.
  1. +6 −4 lib/nserve.js
  2. +48 −0 samples/ajax.html
  3. +4 −0 samples/jquery.min.js
  4. +5 −3 src/nserve.coffee
View
@@ -69,21 +69,23 @@
app.get('/ws/:file', function(req, res, next) {
res.writeHead(200, {
'Content-Type': "text/plain",
- 'Access-Control-Allow-Origin': '*'
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Headers': 'X-Requested-With'
});
return res.end(req.params.file);
});
return app.post('/ws', function(req, res, next) {
res.writeHead(200, {
'Content-Type': "text/plain",
- 'Access-Control-Allow-Origin': '*'
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Headers': 'X-Requested-With'
});
- return res.end("post success");
+ return res.end("post result");
});
});
};
_init = function() {
- return connect.createServer(connect.favicon(), _router(), connect.directory(process.cwd()), fileTransfer(_rate, _fileTransferCallback));
+ return connect.createServer(_router(), connect.favicon(), connect.directory(process.cwd()), fileTransfer(_rate, _fileTransferCallback));
};
/* bootstrap */
_version();
View
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Ajax Test</title>
+ <script type="text/javascript" src="jquery.min.js"></script>
+</head>
+<body>
+ <p>Open this page locally or run through other web server. You will see that the cross-domain ajax calls will work. This will be very handy for local development, because you will not need to change anything in your code, and it will just work as if they were same-domain ajax calls.</p>
+
+ <p id="message">This is message will be updated</p>
+ <button id="get_button">Get Request</button>
+ <button id="post_button">Post Request</button>
+ <script type="text/javascript">
+ var msg = document.getElementById("message");
+ var getBtn = document.getElementById('get_button');
+ var postBtn = document.getElementById('post_button');
+
+ getBtn.onclick = function(e) {
+ e.preventDefault();
+ $.ajax({
+ url: "http://localhost:3000/ws/requested_file",
+ type: "get",
+ success: function(data) {
+ msg.innerHTML = "get success: " + data;
+ },
+ error: function(err) {
+ msg.innerHTML = "error: " + err;
+ }
+ });
+ };
+
+ postBtn.onclick = function(e) {
+ e.preventDefault();
+ $.ajax({
+ url: "http://localhost:3000/ws",
+ type: "post",
+ success: function(data) {
+ msg.innerHTML = "post success: " + data;
+ },
+ error: function(err) {
+ msg.innerHTML = "error: " + err;
+ }
+ });
+ };
+
+ </script>
+</body>
+</html>
View
Oops, something went wrong.
View
@@ -75,20 +75,22 @@ _router = ->
res.writeHead 200, {
'Content-Type': "text/plain"
'Access-Control-Allow-Origin': '*' # for cross-domain ajax
+ 'Access-Control-Allow-Headers': 'X-Requested-With'
}
res.end req.params.file
-
+
app.post '/ws', (req, res, next) ->
res.writeHead 200, {
'Content-Type': "text/plain"
'Access-Control-Allow-Origin': '*' # for cross-domain ajax
+ 'Access-Control-Allow-Headers': 'X-Requested-With'
}
- res.end "post success"
+ res.end "post result"
_init = () ->
connect.createServer(
- connect.favicon(),
_router(),
+ connect.favicon(),
connect.directory(process.cwd()),
fileTransfer(_rate, _fileTransferCallback)
)

0 comments on commit 67c9f1a

Please sign in to comment.