nodejs_notes's People
nodejs_notes's Issues
nodeJS的promise学习小记
promise主要是应用于异步操作,以及回调等;
bluebird是一个promise库,可以执行 npm install bluebird进行下载
//异步操作
var ids = [1,2,3,4];
var arr=[];//假设arr是一个收集多个http请求回来的数据数组存放的变量
var Promise = require(‘bluebird’);
function promise_test(url){
//promise的方法必须返回一个promise的对象
return new Promise(function(resolve,reject){
//具体操作
//resolve(html) 正确处理,返回数据
//reject() 异常处理
})
}
ids.forEach(id){
arr.push(promise_test(baseUrl + id));
}
Promise.all(arr) //all方法是在等待arr的promise操作的完成,完成之后去执行then方法
.then(function(arr){
arr.forEach(html){
//具体操作
}
})
/_demo2_/
var Promise = window.Promise;
function step1(){
return new Promise(function (resolve,reject){
/* body... */
alert('第1步');
resolve({flag:false});//返回数据操作
})
}
function step2(param){
return new Promise(function (resolve,reject){
/* body... */
alert('第2步');
resolve({flag2:true});//执行下一步,并返回数据
// reject();//不执行下一步
});
}
function step3(param){
return new Promise(function (resolve,reject){
/* body... */
alert('第3步');
resolve();//返回数据操作
})
}
step1()
.then(function (data) {//then方法需要返回一个promise对象,回等到前面的动作结束才执行
/* body... */
// console.log(data);
return step2(data);
})
.then(function (data) {
/* body... */
// console.log(data);
return step3(data);
})
NodeJS安装express遇到报错解决
今天运行一个文件 node ****.js 报错:cann't find moudle express
需要安装express, cmd打开输入命令npm install express -gd(为全局安装,并设置path)报错
解决:
输入命令:npm config set registry http://registry.cnpmjs.org
将原本的registry = "https://registry.npmjs.org/"修改掉,然后执行npm install express -gd(为全局安装,并设置path)即可,npm info ok即安装成功.
注释:npm config ls -l 可查看信息配置
node ***.js文件还是报查找不到,复制下载的express一份放在文件项目中
在windows环境下基于sublime text3的node.js开发环境搭建
第一步:(安装node.js)到http://nodejs.org/下载node,js,然后进行傻瓜式安装。
第二步:(安装SublimeText-Nodejs插件),打开sublime text3,接着找到菜单tools-->build System-->new build System 打开文件后写入
{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell":true,
"encoding": "cp936",
"windows":
{
"cmd": ["taskkill","/F", "/IM", "node.exe","&","node", "$file"]
},
"linux":
{
"cmd": ["killall node; node", "$file"]
}
}
然后将文件保存命名为 Nodejs.sublime-build
第三步: 打开sublime text3,接着找到菜单preferences-->browse packages 打开User目录,将文件Nodejs.sublime-build放入此目录中,然后重启SublimeText
第四步: 建一个test.js放入代码
var http = require('http');
var os = require('os');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
第五步 : Ctrl+b编译这段代码之后,sublime text窗口中就会显示
Server running at http://127.0.0.1:3000/
若之前有运行的node进程在,则会先杀掉node进程,再启动node,显示如下:
成功: 已终止进程 "node.exe",其 PID 为 154588。
Server running at http://127.0.0.1:3000/
到此,服务端算是启动成功,打开浏览器,输入http://127.0.0.1:3000/,页面显示Hello World则表示交互正常。
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.