Skip to content

Instantly share code, notes, and snippets.

@cayasso
Created August 6, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cayasso/a3949bd71aa23467322b to your computer and use it in GitHub Desktop.
Save cayasso/a3949bd71aa23467322b to your computer and use it in GitHub Desktop.
Node JS API test
<!DOCTYPE>
<html>
<head>
</head>
<body>
<a id="eushell">EUSHELL</a>
<a id="cayasso">CAYASSO</a>
<div id="result"></div>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<script>
$('a').on('click', function (e) {
var user = e.target.id;
console.log(e);
$.post('http://localhost:8081/account', { user: user }, function (res) {
$('#result').html(JSON.stringify(res));
});
});
</script>
</body>
</html>
{
"name": "test",
"version": "0.1.0",
"dependencies": {
"body-parser": "^1.6.1",
"express": "^4.8.1"
}
}
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var port = 8081;
app.use(bodyParser());
app.use(express.static(__dirname + '/public'));
var accounts = {
cayasso: {
name: {
first: 'Jonathan',
last: 'Brumley'
},
age: 32
},
eushell: {
name: {
first: 'Eushell',
last: 'Brumley'
},
age: 14
}
};
app.post('/account', function (req, res) {
console.log(req.body)
var user = req.body.user;
res.json(accounts[user]);
});
app.listen(port, function () {
console.log('listening on port %d', port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment