我想通过使用express在Node js中创建一个RESTfull API。在ES5中创建它没有问题,因为我在ES6中有很多问题,因为我想使用类和其他OOP概念。我不知道如何组织它。
如果我有服务器.js
const express = require('express');
const router = express.Router();
const api = new Api(router); //can you please say Can I have an Api class here which will determine routes;
在Api.js公司我想要这样的东西
class Api() {
constructor(expressRouter) {
this.app = expressRouter;
this.determineRoutes();
}
determineRoutes() {
this.app.get('/user', new User().create ); //Can you please note, can I have a User class here which will be a controller ????
this.app.get('/profile', new Profile().create); //The same here
}
}
//or at the end can I write something like this, create a singleton class ????
module.exports = new Api(router);