我想在一个单独的文件中分离我的Mongoose模型.我试图这样做:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Material = new Schema({
name : {type: String, index: true},
id : ObjectId,
materialId : String,
surcharge : String,
colors : {
colorName : String,
colorId : String,
surcharge : Number
}
});
var SeatCover = new Schema({
ItemName : {type: String, index: true},
ItemId : ObjectId,
Pattern : String,
Categories : {
year : {type: Number, index: true},
make : {type: String, index: true},
model …
Run Code Online (Sandbox Code Playgroud) 我正在使用express 3.x和mongoose 3.6.4.
到目前为止,我一直在我的express apps.js文件中创建一个mongoose连接,如下所示:
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/mytest');
Run Code Online (Sandbox Code Playgroud)
然后我将我的模型分成单独的文件,如models/user,如下所示:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var userSchema = new Schema({
userId: ObjectId,
givenName:String ...
});
exports.User = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
然后在多个路由文件中,我使用了User模型,如下所示:
var User = require('./models/user').User;
User.findById(id, function(err, user){ ...
Run Code Online (Sandbox Code Playgroud)
这对当地的发展很有帮助.所有型号共享相同的连接.
但是现在我想使用mongoose.createConnection()并创建一个可以类似方式使用的连接池.我想在应用程序启动时建立一个连接池,并为所有模型使用池.所以,我试过这个:
var conn = mongoose.createConnection("localhost","dpatest", 27017,{user:"???",pass:"???"});
Run Code Online (Sandbox Code Playgroud)
但模型不会自动共享连接.所以,我尝试了各种各样的东西,比如导出连接并尝试在模型中使用它.但除了为每个模型建立单独的连接之外,没有任何其他工作.
有人可以解释如何使用mongoose.createConnection()创建单个连接池吗?
以下是关于我的问题的更多信息:
/************ app.js *****************/
var http = require('http');
var path = require('path');
var express = require('express');
//these are my routing files
var auth = require('./src/auth'); …
Run Code Online (Sandbox Code Playgroud) 如果我使用es6/7(babel - stage 1)而不是TypeScript,那么如何注入服务,特别是Http?
这是我的组件JS:
import {Component, Inject, View, CORE_DIRECTIVES, ViewEncapsulation} from 'angular2/angular2';
import {Http} from 'angular2/http';
@Component({
selector: 'login'
})
@View({
templateUrl: './components/login/login.html',
styleUrls: ['components/login/login.css'],
directives: [CORE_DIRECTIVES],
encapsulation: ViewEncapsulation.Emulated
})
export class Login {
constructor(@Inject(Http) http) {
console.log('http', http);
}
authenticate(username, password) {
// this.http.get('/login');
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
export class Login {
constructor(@Inject(Http) http) {
console.log('http', http);
}
}
/********************/
@Inject(Http)
export class Login {
constructor(http) {
console.log('http', http);
}
}
/********************/
export class Login {
constructor(Http: http) { …
Run Code Online (Sandbox Code Playgroud) 我是webcomponents的新手.由于web组件的v1可用,我从那里开始.我已经在网上阅读了关于他们的各种帖子.我对正确编写它们特别感兴趣.我已经阅读了有关插槽并让它们正常工作的信息,尽管我的努力并未导致按照我的预期方式工作的插槽web组件.
如果我编写了这样的嵌套Web组件,嵌套/开槽web组件中的DOM不会插入父级的插槽中:
<parent-component>
<child-component slot="child"></child-component>
</parent-component>
Run Code Online (Sandbox Code Playgroud)
这是父webcomponent HTML:
<div id="civ">
<style>
</style>
<slot id="slot1" name="child"></slot>
</div>
Run Code Online (Sandbox Code Playgroud)
由于每个web组件(父组件和子组件)都是独立的,因此我一直在创建它们:
customElements.define('component-name', class extends HTMLElement {
constructor() {
super();
this.shadowRoot = this.attachShadow({mode: 'open'});
this.shadowRoot.innterHTML = `HTML markup`
}
})
Run Code Online (Sandbox Code Playgroud)
生成的DOM包括2个阴影根.我试图编写没有阴影dom的子/槽元素,这也不会导致托管孩子的父阴影dom.
那么,构建v1嵌套web组件的正确方法是什么?
我有一个在端口3100上运行的Express.js(v 2.5.8)(节点v0.6.12)服务器.它由Nginx前端,它将http和https请求代理到端口3100.
我想通过https强制某些网址.这是一个例子(app是我的Express服务器):
app.get('/applyNow', ensureSec, secure.showApplication );
Run Code Online (Sandbox Code Playgroud)
ensureSec是我试图用来检查连接是否超过ssl的函数:
function ensureSec(req, res, next) {
if (req.session.ssl == true) {
return next();
} else {
req.session.ssl = true;
res.redirect('https://' + url.parse(req.headers.referer).host +
url.parse(req.url).pathname);
}
}
Run Code Online (Sandbox Code Playgroud)
重定向工作,但节点(超时后)抛出一个错误,说"不能GET/applyNow
重定向到ssl的正确方法是什么?
我想将Express的环境传递给Express的路由模块.我想关闭Express是否在开发或生产模式下运行.为此,我猜我需要以某种方式将app.settings.env传递给路由模块.
我的路由模块为每条路由导出一个函数.所以:
app.get('/search', web.search);
Run Code Online (Sandbox Code Playgroud)
基于之前的stackoverflow帖子,我试过这个:
var web = require('./web')({'mode': app.settings.env});
Run Code Online (Sandbox Code Playgroud)
但是节点抛出了类型错误(对象不是函数).
我是Node和Express的新手.我可以将值传递给快速路线吗?如果是,如何传递?
为什么这不起作用:
<input type="text" name="givenName" <% if(givenName) {%> value="<%= givenName %>" <% } %>/><br/>
Run Code Online (Sandbox Code Playgroud)
它抛出一个引用错误,指出givenName 未定义,这可能不是,并且是条件的原因。
我已经阅读并重新阅读了几篇关于Mongoose中嵌入和链接文档的帖子.根据我所读到的内容,我得出结论,最好使用类似于以下的模式结构:
var CategoriesSchema = new Schema({
year : {type: Number, index: true},
make : {type: String, index: true},
model : {type: String, index: true},
body : {type: String, index: true}
});
var ColorsSchema = new Schema({
name : String,
id : String,
surcharge : Number
});
var MaterialsSchema = new Schema({
name : {type: String, index: true},
surcharge : String,
colors : [ColorsSchema]
});
var StyleSchema = new Schema({
name : {type: String, index: true},
surcharge : String,
materials : …
Run Code Online (Sandbox Code Playgroud) 我对 Node.js 比较陌生。我正在尝试将 83 个大小约为 400MB 的 XML 文件转换为 JSON。
每个文件都包含这样的数据(除了每个元素都有大量的附加语句):
<case-file>
<serial-number>75563140</serial-number>
<registration-number>0000000</registration-number>
<transaction-date>20130101</transaction-date>
<case-file-header>
<filing-date>19981002</filing-date>
<status-code>686</status-code>
<status-date>20130101</status-date>
</case-file-header>
<case-file-statements>
<case-file-statement>
<type-code>D10000</type-code>
<text>"MUSIC"</text>
</case-file-statement>
<case-file-statement>
<type-code>GS0351</type-code>
<text>compact discs</text>
</case-file-statement>
</case-file-statements>
<case-file-event-statements>
<case-file-event-statement>
<code>PUBO</code>
<type>A</type>
<description-text>PUBLISHED FOR OPPOSITION</description-text>
<date>20130101</date>
<number>28</number>
</case-file-event-statement>
<case-file-event-statement>
<code>NPUB</code>
<type>O</type>
<description-text>NOTICE OF PUBLICATION</description-text>
<date>20121212</date>
<number>27</number>
</case-file-event-statement>
</case-file-event-statements>
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的 Node 模块,包括 sax、node-xml、node-expat 和 xml2json。显然,我需要从文件中流式传输数据并通过 XML 解析器将其通过管道传输,然后将其转换为 JSON。
我还尝试阅读了一些博客等,试图解释如何解析 Xml,尽管是表面的。
在 Node 世界中,我首先尝试了 sax,但我不知道如何以可以将其转换为 JSON 的格式提取数据。xml2json 不适用于流。node-xml 看起来令人鼓舞,但我无法弄清楚它如何以任何有意义的方式解析块。node-expat 指向 libexpat 文档,该文档似乎需要博士学位。Node elementree 做同样的事情,指向 Python 实现,但没有解释已经实现的内容或如何使用它。
有人可以指出我可以用来开始的例子吗?
我正在玩v1网络组件。根据Eric Bidelman在“ 自定义元素v1:可重用的Web组件”中所述,可以使用以下方式定义和创建v1 Web组件:
class App extends HTMLElement {
attachedCallback() {
this.attachShadow({mode: 'open'});
this.shadowRoot.innterHTML = `<div>web component</div>`
}
}
Run Code Online (Sandbox Code Playgroud)
和
<x-app></x-app>
Run Code Online (Sandbox Code Playgroud)
但是,在最新版本的Chrome Canary中运行代码并在Firefox中使用v1 polyfill时,会引发以下错误:
Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
Run Code Online (Sandbox Code Playgroud) 如何构造HAPROXY配置文件来阻止对特定HTTP方法的请求?
我们开始看到许多攻击使用我们的应用程序中不支持的方法.我们宁愿拒绝我们的负载均衡器的流量,而不是让我们的应用程序陷入困境.
绘制对象并用鼠标修改对象后,坐标(Object.width 和 Object.height)保持与最初绘制的对象相同。
const button = document.querySelector('button');
function load() {
const canvas = new fabric.Canvas('c');
const rect = new fabric.Rect({
width: 100,
height: 100,
left: 10,
top: 10,
fill: 'yellow',
});
function objectAddedListener(ev) {
let target = ev.target;
console.log('left', target.left, 'top', target.top, 'width', target.width, 'height', target.height);
}
function objectMovedListener(ev) {
let target = ev.target;
console.log('left', target.left, 'top', target.top, 'width', target.width, 'height', target.height);
}
canvas.on('object:added', objectAddedListener);
canvas.on('object:modified', objectMovedListener);
canvas.add(rect);
}
load();
button.addEventListener('click', load);
Run Code Online (Sandbox Code Playgroud)
见代码笔
我的应用程序使用节点0.10.1,表达3.1.1,mongoose 3.6.4,mongo 2.4.1和gridfs-stream 0.4.0.
我已经使用共享连接设置了mongoose和gridfs-stream,如下所示:
/************* app.js ***************/
//Added this in edit to show setup of mongoose and gridfs-stream
var mongoose = require("mongoose");
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
global.conn = mongoose.createConnection(dbUri);
conn.once('open', function(){
global.gfs = Grid(conn.db);
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用gridfs-stream上传/下载文件.我上传的内容如下:
exports.uploadFile = function(req, res){
var file = req.files.upload;
var docId = mongoose.Types.ObjectId(req.query.docId);
var filename = req.query.ileName;
var contentType = file.type;
if(!file) return res.send({result: 'NO_FILE_UPLOADED'});
var writestream = gfs.createWriteStream({
_id: docId,
filename: filename,
mode: 'w',
root: 'documents'
});
// more here …
Run Code Online (Sandbox Code Playgroud) node.js ×7
express ×5
javascript ×4
mongoose ×4
mongodb ×2
angular ×1
babeljs ×1
ejs ×1
fabricjs ×1
gridfs ×1
haproxy ×1
json ×1
shadow-dom ×1
xml-parsing ×1