Igor Suvorov
Программист-предприниматель
12 Nov 2016
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
JWT.IO allows you to decode, verify and generate JWT.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1N9Cj0JI1Njk4NjY3NdCkMDAwMTIw0KM3MNCjMCIsImVtYWlsIjoiVmljdG9yLk1heW9yb3ZAZ21haWwuY29tIiwibmFtZSI6ItCS0LjQutGC0L7RgCDQnNCw0LnQvtGA0L7QsiIsImlhdCI6MTQ3NTE3MjAxNn0.z2r4E-CXKDdEnk-rMUlmHadul78CallopAED7kZS-uY
150ms
ssd 100ms
hdd 300ms
FS
MONGO
5ms
10ms
Redis
JWT decode
router.get("/", function (req, res) {
return myGetOperation()
.then(myOtherOperation);
});
router.post("/:test", function (req, res) {
return myParametrizedOperation(req.params.test)
.then(myOtherOperation);
});
router.use(function (req, res) {
return myMiddlewareOperation()
.then(myOtherOperation);
});
api.get('/offer', async() => {
return Offer.find();
})
api.get('/offer/:id', async(req) => {
return Offer.check(req.params.id)
})
api.get('/offer/category/:id', async(req) => {
return Offer.findByCategoryId(req.params.id)
})
api.get('/404', async(req) => {
throw new Error('404 Not Found')
})
/api
{
"message": "Current api is here: /api/v2",
"url": "/api/v2"
}
/api/v2
{
"code": 0,
"message": "ok",
"title": "api",
"version": 2,
"docs": "/api/v2/docs",
"docsJson": "/api/v2/docs/json"
}
> npm install bunyan
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myapp'});
log.info('hi');
log.warn({lang: 'fr'}, 'au revoir');
$ node hi.js
{"name":"myapp","hostname":"banana.local","pid":40161,"level":30,"msg":"hi","time":"2013-01-04T18:46:23.851Z","v":0}
{"name":"myapp","hostname":"banana.local","pid":40161,"level":40,"lang":"fr","msg":"au revoir","time":"2013-01-04T18:46:23.853Z","v":0}
var bunyan = require('bunyan');
var log = bunyan.createLogger({
name: <string>, // Required
level: <level name or number>, // Optional, see "Levels" section
stream: <node.js stream>, // Optional, see "Streams" section
streams: [<bunyan streams>, ...], // Optional, see "Streams" section
serializers: <serializers mapping>, // Optional, see "Serializers" section
src: <boolean>, // Optional, see "src" section
// Any other fields are added to all log records as is.
foo: 'bar',
...
});
> babel-node index.js
| ./bunyan -o short -l trace
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
> package.json
> "test": "mocha \"src/**/*.test.js\"
--require babel-register
--require test/setup.js",
var request = require('supertest');
var express = require('express');
var app = express();
app.get('/user', function(req, res) {
res.status(200).json({ name: 'tobi' });
});
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200)
.end(function(err, res) {
if (err) throw err;
});
describe('GET /user', function() {
it('respond with json', function(done) {
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
статьте лайки, подписывайтесь на канал
telegram.me/isuvorov
vk.com/igor.suvorov
github.com/isuvorov
By Igor Suvorov
* JSON Web Token * Express-async-router * Swagger * Bunyan * Tests