Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

How to return a pdf download file instead of data ? #125

Closed
rimiti opened this issue Jul 21, 2017 · 0 comments
Closed

How to return a pdf download file instead of data ? #125

rimiti opened this issue Jul 21, 2017 · 0 comments

Comments

@rimiti
Copy link

rimiti commented Jul 21, 2017

Hi,

Sorry to put an issue but I don't have found a response to my answer.
Can you say me how I can return a file instead of data in a service method ?

I have tried to access on the express request method but it doesn't work... Do you know how I can make it ?

Version
feathers-rest: 1.2.3
node: 6.11.1
npm: 5.3.0

Server configuration

require('source-map-support').install() //For mapping errors from es5 to es6
import feathers from 'feathers'
import rest from 'feathers-rest'
import bodyParser from 'body-parser'
import services from './services'
import hooks from 'feathers-hooks'
import configuration from 'feathers-configuration'
import sequelize from './db'
import swaggerObject from '../api/2.0/swagger.json'
import jwtCheck from './middlewares/jwtCheckMiddleware'
import {handler} from './modules/errors/handler'
import uaparser from 'ua-parser-js'
import {default as logger, errorLog} from './lib/logger'
// import AWS from 'aws-sdk'
import validationErrorHandler from './middlewares/validationErrorHandler'
import compression from 'compression'
import packageJson from '../package.json'
import config from './config'
import environmentMiddleware from './middlewares/environmentMiddleware'
let initializeSwagger = require('swagger-tools').initializeMiddleware

sequelize.authenticate()
	.then(() => console.log('Connection has been established successfully.'))
	.catch((e) => console.error('Unable to connect to the database', {error: e}))


// Configure non-Swagger related middleware and server components prior to Swagger middleware
initializeSwagger(swaggerObject, (swaggerMiddleware) => {

	let app = feathers()
	app.use(bodyParser.json({limit: '5mb'}))
	app.use(environmentMiddleware)
	app.use(swaggerMiddleware.swaggerRouter({controllers: './dist/services'}))

	// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
	app.use(swaggerMiddleware.swaggerMetadata())

	// Provide the security handlers
	app.use(swaggerMiddleware.swaggerSecurity({
		oauth2: (req, def, scopes, callback) => {
		}
	}))

	// Validate Swagger requests
	app.use(swaggerMiddleware.swaggerValidator({validateResponse: false}))

	// Return custom headers
	app.use((req, res, next) => {
		res.header("Access-Control-Allow-Origin", "*")
		res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
		res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-g8-device-name, x-g8-device-Uid, X-g8-masterkey, x-g8-environment, x-g8-device-uid, x-g8-refresh-token")
		res.header("x-powered-by", `g8 ${packageJson.version}`)
		next()
	})

	// Compress all requests
	app.use(compression())

	// Serve the Swagger documents and Swagger UI
	app.use(swaggerMiddleware.swaggerUi({swaggerUi: "/swagger"}))

	app.configure(rest())
		.use(jwtCheck)
		.use(services)
		.use((req, res, next) => {
			req.feathers.useragent = uaparser(req.headers['user-agent'])
			next()
		})
		.use((req, res, next) => {
			req.feathers.headers = req.headers
			next()
		})
		.configure(hooks())
		.configure(configuration(__dirname, 'config'))

	//intercept swagger validation tool errors
	app.use(validationErrorHandler)
	app.use(handler)
	app.listen(3000)

})

Invoice service

export default {
	/**
	 * @description Download user company order invoice
	 * @param data - passed data from request
	 * @param params - params send with the request
	 * @param callback
	 * @returns {Promise}
	 */
	create(data, params, callback){
		// how to return a pdf download file ?
	},
	setup(app, path){
		this.app = app
		//Bind the apps service method to service to always look services up dynamically
		this.service = app.service.bind(app)
		let h = (service) => {
			return (hook) => {
				if (hook.type === 'before') {
					console.time(`${hook.method}:${service}`)
				} else if (hook.type === 'after') {
					console.timeEnd(`${hook.method}:${service}`)
				}
			}
		}
		this.before({all: h('users/companies/orders/invoices/root')})
		this.after({all: h('users/companies/orders/invoices/root')})
	}
}

Thank you so much for you help.
Regards,

@rimiti rimiti closed this as completed Jul 21, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant