我正在尝试使用node.js编写脚本来下载图像.这是我到目前为止:
var maxLength = 10 // 10mb
var download = function(uri, callback) {
http.request(uri)
.on('response', function(res) {
if (res.headers['content-length'] > maxLength*1024*1024) {
callback(new Error('Image too large.'))
} else if (!~[200, 304].indexOf(res.statusCode)) {
callback(new Error('Received an invalid status code.'))
} else if (!res.headers['content-type'].match(/image/)) {
callback(new Error('Not an image.'))
} else {
var body = ''
res.setEncoding('binary')
res
.on('error', function(err) {
callback(err)
})
.on('data', function(chunk) {
body += chunk
})
.on('end', function() {
// What about Windows?!
var path = '/tmp/' + …Run Code Online (Sandbox Code Playgroud) 我对正则表达式不太满意,所以我想确保我正确地做到这一点.假设我有两条非常相似的路线,/discussion/:slug/并且/page/:slug/.我想创建一个匹配这两个页面的路由.
app.get('/[discussion|page]/:slug', function(req, res, next) {
...enter code here...
})
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?现在我只是创建了两条不同的路线.
someFunction = function(req, res, next) {..}
app.get('/discussion/:slug', someFunction)
app.get('/page/:slug', someFunction)
Run Code Online (Sandbox Code Playgroud) 我有一个"无限滚动"的页面.它会计算页面末尾与当前页面之间的差异,如果此差异足够小,则会加载更多内容.使用jQuery代码是这样的:
$(window).on('scroll', function() {
if (window.pageYOffset > loadMoreButton.offsetTop - 1000)
# load more content via ajax
}
Run Code Online (Sandbox Code Playgroud)
现在,问题在于每次滚动时,每个滚动都会触发此事件多次.我想每隔x毫秒发射一次.我该怎么做?
我希望在一个Pyramid实例上有多个域和子域.但是,我似乎无法找到任何文件.最后一个问题涉及一个资料很少,没有例子的词汇表.你们有没有任何例子或者可以指导我更好的文档?
我正在尝试通过make在顶级目录中简单执行来构建我的项目.但是,当我这样做时,我收到以下错误:
[Errno 2] No such file or directory
[cmd: [u'make']]
[dir: /Users/jonathanong/Workspace/template]
[path: /usr/local/bin]
[Finished]
Run Code Online (Sandbox Code Playgroud)
这是在将构建配置设置为之后Make.
我在Sublime Text 2.0.1,OS X 10.8.2上.我的Makefile包含执行全局安装的node.js二进制文件.我需要做什么?
我只是学习如何使用SQLAlchemy.我正在尝试执行以下操作,但将标题和链接存储在两个单独的表中:
temp = Submissions(title=u'Facebook Homepage', link=u'http://facebook.com')
session.add(temp)
session.flush()
transaction.commit()
Run Code Online (Sandbox Code Playgroud)
通过:
class Links(Base):
__tablename__ = 'links'
id = Column(Integer, primary_key=True)
link = Column(Text)
created = Column(TIMESTAMP(), default=datetime.now())
def __init__(self, link):
self.link = link
class Submissions(Base):
__tablename__ = 'submissions'
id = Column(Integer, primary_key=True)
created = Column(TIMESTAMP(), default=datetime.now())
title = Column(Text)
link_id = Column(Integer, ForeignKey('links.id'))
link = relation(Links)
def __init__(self, title, link):
self.title = title
self.link = link
Run Code Online (Sandbox Code Playgroud)
但是,我总是得到这个错误:
AttributeError: 'unicode' object has no attribute '_sa_instance_state'
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?有没有更好的方法来编码?
我有这些表格表:
class Thing(Base):
__tablename__ = 'thing'
id = Column(Integer, primary_key=True)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Voteinfo(Base):
__tablename__ = 'voteinfo'
thing_id = Column(Integer, ForeignKey('thing.id'), primary_key=True)
thing = relationship('Thing', backref='voteinfo')
upvotes = Column(Integer)
downvotes = Column(Integer)
def __init__(self, thing)
self.thing = thing
class VoteThing(Base):
__tablename__ = 'votething'
id = Column(Integer, primary_key=True)
voter_id = Column(Integer, ForeignKey('voter.id'))
voter = relationship('Voter', backref='votescast')
thing_id = Column(Integer, ForeignKey('thing.id'))
thing = relationship('Thing', backref='votesreceived')
value = Column(Boolean)
def __init__(self, voter, thing, value):
if value …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个记录响应时间和状态代码并将其发送到数据库的中间件.但是,我不确定要使用什么事件.在节点的文档中有一个close事件但它永远不会被触发.end也不起作用.但是,header确实如此,但我找不到任何文档.
app.use(function(req, res, next) {
res.on('close', function() {
console.log('close')
})
res.on('end', function() {
console.log('end')
})
res.on('header', function() {
console.log('header')
console.log(res.statusCode)
})
next()
})
Run Code Online (Sandbox Code Playgroud)
只有header触发,它确实返回正确的res.statusCode.
我的问题:
close解雇?为什么要header解雇?我正在尝试将CSV中的数据导入到表中.问题在于,即使使用CSV HEADER,也会根据列索引导入CSV,而不是在该列的标题上导入.
CREATE TABLE denominations (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE churches (
id SERIAL PRIMARY KEY,
-- NOT relevant here
address_id INTEGER REFERENCES addresses,
denomination_id INTEGER NOT NULL REFERENCES denominations,
name VARCHAR(100) NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
我的CSV看起来像:
id,name
1,Southern Baptist Convention
2,Nondenominational
3,Catholic
4,Presbyterian
id,denomination_id,name,address_id
1,1,Saddleback Church,
2,4,First Presbyterian Church,
3,3,St. Elizabeth's Church,
4,3,St Monica Catholic Community,
5,2,Modern Day Saints Church,
6,4,Second Presbyterian Church,
Run Code Online (Sandbox Code Playgroud)
我的COPY命令在bash中看起来像这样:
psql -d vacation -c "COPY denominations FROM …Run Code Online (Sandbox Code Playgroud) node.js ×3
python ×3
sqlalchemy ×3
pyramid ×2
css ×1
csv ×1
download ×1
express ×1
image ×1
javascript ×1
makefile ×1
postgresql ×1
regex ×1
sublimetext ×1
sublimetext2 ×1