Tag: node.js

访问节点ReadStream

我有一个包裹在承诺中的代码片段。 这段代码从http读取图像,做各种事情,最后把它发送到aws.s3.putObject。 看起来像这样(简化):请注意forms是多方对象。 form.on('part', (part) => {//form is multiparty fileCount++; let tmpFile = path.join(os.tmpDir(), `${userId}_${timePrefix}_${path.basename(part.filename)}`); part.pipe(fs.createWriteStream(tmpFile)); part.on('end', () => { resolve(fs.createReadStream(tmpFile)); }); part.once('error', (error) => { handleError(error); }); }); form.once('error', (error) => { handleError(error); }); form.parse(request); }).then((imageStream) => { //here is a call to AWS S3.putObject. Which return a promise }).then(() => { return new […]

React + Express + Nodemailer:无法将组件和Nodemailer渲染在一起

我想使用客户端路由来呈现我的React应用程序,并且想要使用Nodemailer发送邮件。 由于Nodemailer不能在客户端使用,我必须在Express服务器上实现此function。 这就是服务器的样子: express.js router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, '..', 'build/index.html')) }) router.get('/sendmail', (req, res) => { transporter.sendMail(options, (error, info) => { if (error){ console.log(error) } console.log('Message was send!') }) }) 1)所以这呈现了React组件,但是当我路由到'/ sendmail'时显示一个空白页面,index.html被渲染为没有js,并且没有邮件被发送。 2)如果我删除第一行router.use(express.static…并路由到'/ sendmail'邮件被发送,但我的应用程序不会呈现。 另外我已经尝试了以下内容: router.use(express.static(path.resolve(__dirname, '..', 'build'))) router.get('*', (req, res) => { console.log('path', req.path) if (req.path === '/sendmail') { […]

将图像保存为base64,并使用path返回

我有点新的使用NodeJS,这是我第一次创build一个API,我上传一个angular度和ngfile upload到服务器的图像,这个想法是,API接收一个文件,编码它到base64,然后给我的对象与图像的path,然后显示在客户端。 config.js 'use strict' const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const multer = require('multer'); const fs = require('fs-extra'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); res.header("Access-Control-Allow-Methods", "GET,HEAD,POST,PUT,DELETE"); next(); }); app.use('/uploads', express.static("./uploads")); app.use(bodyParser.json()); var storage = multer.diskStorage({ //multers disk […]

如何用asynchronousvalidation更新mongoose模型(node / koa2)

从客户端,用户有一个表格填写他们可以更新他们的电子邮件或密码。 他们必须input当前密码才能validation请求。 之后,如果电子邮件地址正在被修改,那么电子邮件validation器应该运行以检查新的电子邮件地址是否已经被注册到另一个用户。 所以,在用户从客户端发送请求后,我得到以下信息: { userId: 'This is the ID of their entry in the database', currentPassword: 'Current password to check against one stored in database', newPassword: 'New password, will be blank if user has not requested a new password', email: 'Email address, will be the same if user has not requested a new email' […]

我不能从Node控件和mongodb中使用get方法从html控件中获取数据

代码中的问题 var userID = req.userid; var pwd = req.pwd; console.log("userid = " + userID + "pass = " + pwd); 控制台显示的值未定义,而不是input数据 控制台显示的值未定义,而不是input数据 我想从一个html文件中获取数据,并使用get方法插入到mongo数据库中。 但我无法从文本框中获取数据。 在nodejs(index.js)中的代码 const express = require('express'); const path = require('path'); const bodyparser = require("body-parser"); const mongoose = require('mongoose'); const app = express(); app.use(bodyparser()); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json()); app.set('port', (process.env.PORT || 1000)); […]

脚本和CSS文件不加载在节点JS?

我无法加载节点JS中的jQuery和CSS文件 public javascripts stylesheets 我有HTML文件,我读了在server.js如下 http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/html' }); fs.readFile('Views/index.html', 'utf8', function (error, data) { if (error) { res.writeHead(404); res.write('file not found'); } else { res.write(data); } res.end() }); }).listen(port); 如何正确加载脚本和css文件。

Node.js:如何在不获取UnhandledPromiseRejectionWarning的情况下返回被拒绝的承诺

我有一个使用request-promise-native模块查询couchdb数据库的模块中的函数: userByEmail: (email) => { const options = { url: `${config.couchdb.url}/medlog/_design/user/_view/by_email_or_userid?key="${email}"`, json: true, }; return rp.get(options) .then(users => users.rows.map(row => row.value)) .catch(reason => Promise.reject(new Error('test'))); } 第二个模块包含一个使用第一个模块的函数: router.get('/checkEmailExistence', (req, res) => { couchdb.userByEmail(req.param('email')) .then((userArray) => { res.status(200).end(userArray.length > 0); // returns 'true' if at least one user found }) .catch((e) => { winston.log('error', e.message); res.status(500).end(e.message); […]

在Mongodb Nodejs中分页

是否有任何方法来获得查找操作中的文档总数以及MongoDB查询中的跳过和限制 MongoClient.connect(Config.dbURI, function (err, db) { if (!err) { console.log("We are connected"); console.log(uniqueId) db.collection(dbName).find({'uniqueId': uniqueId, 'isDeleted': false}) .sort({modifiedDateISO: -1}) .limit(parseInt(limit)) .skip(parseInt(limit * page)) .toArray((errFindChat, dataFindChat) => { console.log(errFindChat, dataFindChat); }); });

我的回拨有什么问题?

这是具有2个函数的文件“path-info.js”:pathInfo&callback。 Pathinfo从对象“info”中收集有关文件的所有信息,callback获取该对象并将其返回。 码: "use strict"; const fs = require("fs"); let getInfo = function (err, someObject) { if (err) throw err; return someObject; }; function pathInfo(path, callback) { let info = {}; info.path = path; // write path info fs.stat(path, (err, type) => { // write type info if (type.isFile()) { info.type = "file"; } if […]

为什么mysql.escape会将“hello”parsing为“\”hello \“

我将首先逃避参数,然后在查询时连接sqlstring。 所以当我连接查询string使用like : const name = mysql.escape(req.info.name) const sqlString = `select…name like '%${name}%'` 这将导致select … where name like '%'hello'%' ,但我想要的是select … where name like '%hello%'