1. The Feathers
Feathers: Express-based Web application framework. It provides a series of tools to quickly create extensible REST APIs and real-time applications. The official documentation introduces an application architecture that is easier to understand, maintain, and extend than the traditional MVC architecture. Feathers main method introduction
-
services
Services is the core of the Feathers application and handles data access responsibilities. The main methods of service are:
- Find – Find all data (which may match the query)
- Get – Gets a single data entry by its unique identifier
- Create – Creates new data
- Update – Updates an existing data entry by completely replacing it
- Patch – To update one or more data entries by merging new data
- Remove – Removes one or more existing data entries
The main function of “Services” is the processing of data, including simple adding, deleting, changing and checking. Of course, if we want to improve the API interface (such as adding login authentication), we need to use another point of knowledge
-
hooks
Hooks, which are pluggable middleware features that can be registered before or after a service method feedback occurs. A simple chestnut:
app.service('messages').hooks({ before: { create: async context => { context.data.createdAt = new Date(); returncontext; }}})Copy the code
Well, I basically studied the feathers’ more detailed description through these, of course, we can go to the official documentation for that
2. Installation and use
The configuration environment requires Node 6.0 or later
Of course we can use the Feathers Generator to quickly create an application and install the Feathers Generator
npm install @feathersjs/cli -g
Copy the code
Generate the application
feathers generate app
Copy the code
Here you need to enter a brief description of how to create the application. The detailed description can be explained in the official documentation. Here, after all the preparation work is done, a basic application framework is set up and we can use it directly
npm start
Copy the code
Open localhost:3030 and you’ll see the welcome screen for the created application.
3. Create a new service
A new service is a new data model in Express, such as a user table, which can be quickly created using generators:
feathers generate service
Copy the code
After creating the new service, we can see the newly created data model table under Models in the file directory. We just need to modify the table fields to complete the new data table.
The services folder will also create two JS files, one for the hooks registration file and the other for the Service service file.
In the hooks register file we can write the code we need. For example, login authentication
Logon authentication is only used by hooks, which check all interfaces before accessing them.
const { authenticate } = require('@feathersjs/authentication').hooks;
module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [
content => {
}
],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
Copy the code
4. Login authentication
Login authentication we can also generate it directly from scaffolding:
feathers generate authentication
Copy the code
This allows us to generate interface information for login authentication after simple input.
The default login authentication is email + password. If you need to modify it, you need to go to the default.json file under config to modify it
"local": {
"entity": "users"."usernameField": "phone"// Change this to a personal table field"passwordField": "password"
}
Copy the code
In the authenticated interface, we can find that only token information is returned, if we want to return the corresponding user information:
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies)
],
remove: [
authentication.hooks.authenticate('jwt') ] }, after: { all: [hook => {hook. Result. user = hook. Params.user; // Return user to the front endreturnhook; }]}});Copy the code
summary
It’s a little confusing. Well, the simple creation of the application is basically done. In fact, the official documentation explains it very clearly.
Reference documentation
Featherjs