我在两个步骤中使用了带有nodejs的write文件:
1.首先判断文件是否存在,使用fs.exists
功能;
然后fs.writeFile
直接用来写文件;
但是现在我注意到有更多的函数用于写文件,比如fs.open
或者fs.close
,我应该在写入时使用这些函数来打开还是关闭文件?
此外,我注意到有有fs.createReadStream
和fs.createWriteStream
功能,什么是它们之间的差异fs.writeFile
和fs.readFile
?
<head>
<script async></script>
</head>
<body>
<img />
<img />
<img />
</body>
Run Code Online (Sandbox Code Playgroud)
头部只有一个脚本元素,具有异步属性和2秒延迟,执行3秒.
但Chrome中的网页加载时间轴是:
当脚本执行时,它仍然阻止浏览器渲染过程?
但为什么?它不应该异步执行?
但是它不会阻止解析器:
我运行这样一个简单的烧瓶应用程序:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def welcome():
return "OK"
app.config.update(
DEBUG = True
)
if __name__ == '__main__':
app.run(use_reloader = False)
Run Code Online (Sandbox Code Playgroud)
当我运行并访问它时,有时(并非总是)它无法响应请求并抛出一个除外:
Exception happened during processing of request from ('127.0.0.1', 54481)
Traceback (most recent call last):
File "c:\python27\Lib\SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "c:\python27\Lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "c:\python27\Lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\python27\Lib\SocketServer.py", line 651, in __init__
self.finish()
File "c:\python27\Lib\SocketServer.py", line 710, in finish
self.wfile.close()
File …
Run Code Online (Sandbox Code Playgroud) 我有一个由Immutable.js制作的数组:
var arr = Immutable.List.of(
{
id: 'id01',
enable: true
},
{
id: 'id02',
enable: true
},
{
id: 'id03',
enable: true
},
{
id: 'id04',
enable: true
}
);
Run Code Online (Sandbox Code Playgroud)
我怎样才能找到对象id: id03
?我想更新它的enable
值并获得一个新的数组
我想在dom和css准备就绪时执行一些javascript,但我不关心图像(我倾向于懒加载图像)
我知道如何检测dom就绪状态,但是如何检测css已经准备好了?
无论我是否在变量后定义函数
var a = 1;
function a() {};
typeof a // number
Run Code Online (Sandbox Code Playgroud)
或者如果我在变量之前定义函数
function a() {};
var a = 1;
typeof a // number
Run Code Online (Sandbox Code Playgroud)
最后的typeof
结果总是如此number
我execution context
在http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/找到了一些解释
Before executing the function code, create the execution context.
......
Scan the context for variable declarations:
If the variable name already exists in the variable object, do nothing and continue scanning.
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
那我怎么解释呢?
我在客户端ajax一个GET请求:
$.ajax({
url: '/update',
type: 'get',
data: {
"key": key,
"value": value
},
success: function (data, err) {
}
})
Run Code Online (Sandbox Code Playgroud)
那么在节点方面,我想得到参数
var showParam = function (req, res) {
if (req.body) {
for (var key in req.body) {
console.log(key + ": " + req.body[key]);
}
res.send({status:'ok',message:'data received'});
} else {
console.log("nothing received");
res.send({status:'nok',message:'no Tweet received'});
}
}
app.get('/update', function(req, res){
showParam(req, res);
})
Run Code Online (Sandbox Code Playgroud)
shell显示正文是空的并且未定义.
但是当我更改get
为post
(在客户端和服务器端)时,一切正常,我可以正确获取参数.
我的代码有什么问题?我错过了什么吗?
其实我遇到了两个问题
首先,如何更改上传路径
我的文件夹结构是这样的:
|__app.js
|__upload
Run Code Online (Sandbox Code Playgroud)
我的节点代码在app.js中并从中启动,所以我想上传图片上传到上传文件夹,我改变路径:
var form = new formidable.IncomingForm;
form.uploadDir = "./upload";
Run Code Online (Sandbox Code Playgroud)
它似乎上传成功,但我不知道文件去哪里,它不在上传文件夹中.
那么正确的路径名称是什么?
第二个问题是
如果我不更改它,它可以正确上传到它C:/Users/ADMINI~1/AppData/Local/Temp
但它将在没有foramt的情况下重命名,
那么如何才能获得上传格式并自行重命名?
第三个问题是
我还将处理程序绑定到process
事件,例如
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived + ' ' + bytesExpected);
});
Run Code Online (Sandbox Code Playgroud)
但似乎不起作用,上传日志时没什么.为什么?我错过了什么吗?
这是我的所有代码:
app.post('/upload', function (req, res) {
var form = new formidable.IncomingForm;
// form.uploadDir = "./upload";
console.log(form.uploadDir);
form.parse(req, function(err, fields, files){
if (err) return res.end('You found error');
console.log(files.image);
});
form.on('progress', function(bytesReceived, bytesExpected) {
console.log(bytesReceived + ' ' + bytesExpected);
});
form.on('error', function(err) {
res.writeHead(200, …
Run Code Online (Sandbox Code Playgroud) 在下面的文档中,我可以从Python脚本运行scrapy,但是我无法获得scrapy结果.
这是我的蜘蛛:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from items import DmozItem
class DmozSpider(BaseSpider):
name = "douban"
allowed_domains = ["example.com"]
start_urls = [
"http://www.example.com/group/xxx/discussion"
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
rows = hxs.select("//table[@class='olt']/tr/td[@class='title']/a")
items = []
# print sites
for row in rows:
item = DmozItem()
item["title"] = row.select('text()').extract()[0]
item["link"] = row.select('@href').extract()[0]
items.append(item)
return items
Run Code Online (Sandbox Code Playgroud)
注意最后一行,我尝试使用返回的解析结果,如果我运行:
scrapy crawl douban
Run Code Online (Sandbox Code Playgroud)
终端可以打印返回结果
但是我无法从Python脚本中获得返回结果.这是我的Python脚本:
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy …
Run Code Online (Sandbox Code Playgroud) 最近我读了一些关于angularJS表单验证的教程,如下所示:
<p ng-show="userForm.email.$invalid && !userForm.email.$pristine"></p>
Run Code Online (Sandbox Code Playgroud)
但我认为!$pristine
并且$dirty
是平等的,那么我可以使用下面的那个吗?
<p ng-show="userForm.email.$invalid && userForm.email.$dirty"></p>
Run Code Online (Sandbox Code Playgroud) javascript ×4
node.js ×3
express ×2
python ×2
angularjs ×1
asynchronous ×1
declaration ×1
domready ×1
file ×1
file-upload ×1
flask ×1
formidable ×1
forms ×1
function ×1
get ×1
immutable.js ×1
parameters ×1
scrapy ×1
try-catch ×1
validation ×1
variables ×1
web-scraping ×1