简体   繁体   English

Feathers js - 如何获取 config/default.json 中的数据

[英]Feathers js - how to get the data in config/default.json

I want to extract the host and port from config file(config/default.json etc) in winston-logger.js.我想从 winston-logger.js 中的配置文件(config/default.json 等)中提取主机和端口。
But I cannot find a way to do it, is it possible to do it?但我找不到办法做到这一点,有可能做到吗?

config/default.json配置/默认.json

{
  "host": "localhost",
  "port": 3030,
  "public": "../public/",
  "paginate": {
    "default": 10,
    "max": 50
  },
}

winston-logger.js温斯顿记录器.js

// How to get app.settings in this file?

const logger = expressWinston.logger({
  ...
})

module.exports = logger

app.js应用程序.js

const winstonLogger = require('./winston-logger');
app.use(winstonLogger)

Update更新

When I set the winston-logger.js as suggested by the comment,当我按照评论的建议设置 winston-logger.js 时,

Error occurs.发生错误。

TypeError: app.get is not a function

winston-logger.js温斯顿记录器.js

// How to get app.settings in this file?

const app = require('./app');

console.log(app.get('host'), app.get('port'));

const logger = expressWinston.logger({
  ...
})

module.exports = logger

app.js应用程序.js

const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const logger = require('./logger');
const winstonLogger = require('./winston-logger');

const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');


const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');

const authentication = require('./authentication');

const sequelize = require('./sequelize');

const app = express(feathers());

// Load app configuration
app.configure(configuration());
// Enable security, CORS, compression, favicon and body parsing
app.use(helmet({
  contentSecurityPolicy: false
}));
app.use(cors());
app.use(compress());
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ limit: '10mb', extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', express.static(app.get('public')));

// Set-Up http request logger
app.use(winstonLogger)

// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());

app.configure(sequelize);

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
// Set up event channels (see channels.js)
app.configure(channels);

// Configure a middleware for 404s and the error handler
app.use(express.notFound());
app.use(express.errorHandler({ logger }));

app.hooks(appHooks);

module.exports = app;

These values are available via @feathersjs/configuration through app.get (also see the example ):这些值可通过app.get@feathersjs/configuration获得(另见示例):

// How to get app.settings in this file?

const app = require('./app');

console.log(app.get('host'), app.get('port'));

const logger = expressWinston.logger({
  ...
})

module.exports = logger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM