Like any kind of apps, JavaScript apps also have to be written well.
Otherwise, we run into all kinds of issues later on.
In this article, we’ll look at some best practices we should follow when writing Node apps.
We can authenticate by using JSON web tokens.
A JSON web token consists of 3 parts.
They include:
We can add it easily with some add-ons.
For instance, we can use the koa-jwt package to add the token.
We can write:
const koa = require('koa')
const jwt = require('koa-jwt')
const app = koa()
app.use(jwt({
secret: 'secret'
}))
// Protected middleware
app.use(function *(){
this.body = {
foo: 'bar'
}
})
We just call app.use
to use the jwt
middleware.
The object has the secret to sign the token.
Then we added a protected middleware after that.
The token content will be available with this.state.user
.
The JWT module doesn’t depend on any database layer.
They’re all verified on their own.
They can also contain the time to live values.
To ensure that our communication is secure, we still have to ensure that API endpoints are available through an HTTPS connection.
#programming #javascript #web-development #nodejs