express

Upload file using multer after specific database operation in node js

房东的猫 提交于 2021-01-28 09:02:23
问题 I am using multer to upload files via express . Is there a way to perform a database operation before uploading the file? I need to perform a conditional check whether the user insertion in the database was successful or not depending on that, the file upload should be performed. 回答1: To use multer as per required you can use the following code: var path = require('path'); var multer = require('multer'); var storage = multer.diskStorage({ destination: function(req, file, callback) { callback

Upload file using multer after specific database operation in node js

我的未来我决定 提交于 2021-01-28 08:43:35
问题 I am using multer to upload files via express . Is there a way to perform a database operation before uploading the file? I need to perform a conditional check whether the user insertion in the database was successful or not depending on that, the file upload should be performed. 回答1: To use multer as per required you can use the following code: var path = require('path'); var multer = require('multer'); var storage = multer.diskStorage({ destination: function(req, file, callback) { callback

Send a PDF generated from an API to an external server?

我与影子孤独终老i 提交于 2021-01-28 08:27:05
问题 I have a NodeJS-ExpressJS server (to host a web page) and Django API server. When a specific endpoint in the Django server is called from the ExpressJS server (when a user clicks a button on the web page), a PDF is generated. I want to send this PDF from the Django server to the ExpressJS server and then download it to the user's browser (The user only interfaces with ExpressJS server). In order to do this I encode the PDF as a base64 string in the Django server and return the string in an

Sequelize - Filter FindAll by matching all Tags

痞子三分冷 提交于 2021-01-28 08:10:36
问题 /assets/?tags[]=foo&tags[]=bar Get requests to endpoint above should return only those records that contain ALL of the provided tags ( foo , bar ) My current attempt is returning any records that match ANY of the given tags. const { tags } = req.query; res.send( await Asset.findAll({ where: whereOptions, include: [ { model: AssetMime }, { model: Tag, as: 'tags', where: { title: { [Op.in]: tags, }, }, }, ], }) ); How can I modify my filter to return records where ALL tags match? 回答1: You can

Array not reseting in Node.js

依然范特西╮ 提交于 2021-01-28 08:09:53
问题 I am running a Node+Express API that is working quite well, the only problem I have is when returning a complex query as a response, the variable to which I set the response to is not getting reset between requests. It duplicates replies to the client such as replying 1, 2, 3 then the next reply replies 1, 2, 3, 1, 2, 3. So basically, it's concatenating data and not setting the allPosts variable to null again. Here is my code: var allPosts = []; chirprdb.allPosts = (userData) => { return new

Upload image to heroku using react native

时光怂恿深爱的人放手 提交于 2021-01-28 08:00:57
问题 i am trying to upload image from react native to heroku express app in public/images/ folder. It showing me the success message but there is no image in the images folder. Meanwhile when i try this code on localhost, that works fine.. router.post('/add', (req, res) => { let imageFile = req.files.photo; let fileName = Date.now(); let imgName = fileName+req.files.photo.name; let dirName = __dirname.replace('routes','public') dirName = dirName+'/images/'; let path = dirName+imgName; imageFile.mv

postgrator: not found while migrating postgresql database

*爱你&永不变心* 提交于 2021-01-28 07:44:11
问题 Error: $ heroku run npm run migrate Running npm run migrate on ⬢ shelly-moth-73910... up, run.3979 (Free) > noteful-app-server@1.0.0 migrate /app > postgrator --config postgrator-config.js sh: 1: postgrator: not found npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ERR! errno ENOENT npm ERR! noteful-app-server@1.0.0 migrate: `postgrator --config postgrator-config.js` npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the noteful-app-server@1.0.0 migrate script. npm ERR!

How to download dynamic files from google drive

杀马特。学长 韩版系。学妹 提交于 2021-01-28 07:34:59
问题 noob question, I'm just getting started with Google Drive API v3. How can I download dynamic file from google drive when I only have fileId. file can be, image, pdf, or docs. I tried searching but I couldn't found any reference or example related to this. This what I have so far but it only download specific file extension. downloadFile(req, res) { const auth = new google.auth.JWT( client_email, null, private_key, SCOPES, ); const { fileId } = req.params; const drive = google.drive({ version:

Best way to handle API calls from frontend

て烟熏妆下的殇ゞ 提交于 2021-01-28 06:52:58
问题 Okay, so atm i have a frontend application built with Nuxt JS using Axios to do requests to my REST API(separate). If a user does a search on the website the API URL is visible in XMLHttprequests so everyone could use the API if they want to. What is the best way of making it so that only users that search through my website gets access to the API and people that just directly to the URL gets denied. I suppose using some sort of token system, but what is the best way to do it? JWT? (Users

Jest test are run before DB is ready

二次信任 提交于 2021-01-28 06:52:16
问题 I am using Jest to test my express API with SQLite database, but the following problem appeared - test are run before DB is ready and tables are created. I connect to DB with following code: const connectToDatabase = () => { let db; if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production') { db = new sqlite3.Database('task_list'); } else if (process.env.NODE_ENV === 'test') { db = new sqlite3.Database(':memory:'); } else { throw new Error('Environment is not defined