#node.js #ghost-blog
#node.js #ghost-blog
Вопрос:
Итак, я пытаюсь разместить блог, и я пытаюсь получить доступ к API Ghost, но я не могу придумать способ получить доступ к этому без входа в мою учетную запись Ghost. Вот код, который у меня есть:
var express = require('express'),
api = require('../api'),
apiRoutes;
apiRoutes = function (middleware) {
var router = express.Router();
// ## Posts
router.get('/ghost/api/v0.1/posts', api.http(api.posts.browse));
router.post('/ghost/api/v0.1/posts', api.http(api.posts.add));
router.get('/ghost/api/v0.1/posts/:id(\d )', api.http(api.posts.read));
router.get('/ghost/api/v0.1/posts/:slug([a-z-] )', api.http(api.posts.read));
router.put('/ghost/api/v0.1/posts/:id', api.http(api.posts.edit));
router['delete']('/ghost/api/v0.1/posts/:id', api.http(api.posts.destroy));
// ## Settings
router.get('/ghost/api/v0.1/settings/', api.http(api.settings.browse));
router.get('/ghost/api/v0.1/settings/:key/', api.http(api.settings.read));
router.put('/ghost/api/v0.1/settings/', api.http(api.settings.edit));
// ## Users
router.get('/ghost/api/v0.1/users/', api.http(api.users.browse));
router.get('/ghost/api/v0.1/users/:id/', api.http(api.users.read));
router.put('/ghost/api/v0.1/users/:id/', api.http(api.users.edit));
// ## Tags
router.get('/ghost/api/v0.1/tags/', api.http(api.tags.browse));
// ## Themes
router.get('/ghost/api/v0.1/themes/', api.http(api.themes.browse));
router.put('/ghost/api/v0.1/themes/:name', api.http(api.themes.edit));
// ## Notifications
router.get('/ghost/api/v0.1/notifications/', api.http(api.notifications.browse));
router.post('/ghost/api/v0.1/notifications/', api.http(api.notifications.add));
router['delete']('/ghost/api/v0.1/notifications/:id',api.http(api.notifications.destroy));
// ## DB
router.get('/ghost/api/v0.1/db/', api.http(api.db.exportContent));
router.post('/ghost/api/v0.1/db/', middleware.busboy, api.http(api.db.importContent));
router['delete']('/ghost/api/v0.1/db/', api.http(api.db.deleteAllContent));
// ## Mail
router.post('/ghost/api/v0.1/mail', api.http(api.mail.send));
router.post('/ghost/api/v0.1/mail/test', api.http(api.mail.sendTest));
// #### Slugs
router.get('/ghost/api/v0.1/slugs/:type/:name', api.http(api.slugs.generate));
return router;
};
module.exports = apiRoutes;
Что я должен добавить к этому, чтобы мне не приходилось каждый раз входить в систему для доступа к API?
Ответ №1:
Включите промежуточное программное обеспечение сеанса и задайте переменную, чтобы определить, вошел пользователь в систему или нет.
...
app.use(express.bodyParser());
app.use(express.cookieParser('My secret here'));
app.use(express.session());
app.use(express.static(path.join(__dirname, 'public')));
...
Затем в одном из ваших контроллеров или пользовательском промежуточном программном обеспечении вы можете установить req.session
свойство.
req.session.loggedIn = true;