nodejs01--什么是nodejs,nodejs的基本使用

nodejs使用范围

-直接在cmd命令行运行,在你的电脑上直接运行

-可以搭建一个web服务器(express,koa)

-一些基本的使用

-modules是如何工作的

-npm管理modules

-搭建一个web服务器。

wow,that is awesome

首先当然是下载nodejs的安装包,这里就不再演示了。nodejs官方

modules是如何工作的

建立两个js,moduler01.js moduler02.js

//moduler01js
var _= require('underscore'); var m = require('./modular02.js'); m();
console.log(_);
nodejs
{ [Function]
_: [Circular],
VERSION: '1.8.3',
iteratee: [Function],
forEach: [Function],
each: [Function],
collect: [Function],
map: [Function],
inject: [Function],
foldl: [Function],
reduce: [Function],
foldr: [Function],
reduceRight: [Function],
detect: [Function],
find: [Function],
select: [Function],
filter: [Function],
reject: [Function],
all: [Function],
every: [Function],
any: [Function],
some: [Function],
include: [Function],
includes: [Function],
contains: [Function],
invoke: [Function],
pluck: [Function],
where: [Function],
findWhere: [Function],
max: [Function],
min: [Function],
shuffle: [Function],
sample: [Function],
sortBy: [Function],
groupBy: [Function],
indexBy: [Function],
countBy: [Function],
toArray: [Function],
size: [Function],
partition: [Function],
take: [Function],
head: [Function],
first: [Function],
initial: [Function],
last: [Function],
drop: [Function],
tail: [Function],
rest: [Function],
compact: [Function],
flatten: [Function],
without: [Function],
unique: [Function],
uniq: [Function],
union: [Function],
intersection: [Function],
difference: [Function],
zip: [Function],
unzip: [Function],
object: [Function],
findIndex: [Function],
findLastIndex: [Function],
sortedIndex: [Function],
indexOf: [Function],
lastIndexOf: [Function],
range: [Function],
bind: [Function],
partial: [Function],
bindAll: [Function],
memoize: [Function],
delay: [Function],
defer: [Function],
throttle: [Function],
debounce: [Function],
wrap: [Function],
negate: [Function],
compose: [Function],
after: [Function],
before: [Function],
once: [Function],
keys: [Function],
allKeys: [Function],
values: [Function],
mapObject: [Function],
pairs: [Function],
invert: [Function],
methods: [Function],
functions: [Function],
extend: [Function],
assign: [Function],
extendOwn: [Function],
findKey: [Function],
pick: [Function],
omit: [Function],
defaults: [Function],
create: [Function],
clone: [Function],
tap: [Function],
isMatch: [Function],
isEqual: [Function],
isEmpty: [Function],
isElement: [Function],
isArray: [Function: isArray],
isObject: [Function],
isArguments: [Function],
isFunction: [Function],
isString: [Function],
isNumber: [Function],
isDate: [Function],
isRegExp: [Function],
isError: [Function],
isFinite: [Function],
isNaN: [Function],
isBoolean: [Function],
isNull: [Function],
isUndefined: [Function],
has: [Function],
noConflict: [Function],
identity: [Function],
constant: [Function],
noop: [Function],
property: [Function],
propertyOf: [Function],
matches: [Function],
matcher: [Function],
times: [Function],
random: [Function],
now: [Function: now],
escape: [Function],
unescape: [Function],
result: [Function],
uniqueId: [Function],
templateSettings:
{ evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g },
template: [Function],
chain: [Function],
mixin: [Function] }
//moduler02.js
module.exports = function(){
console.log('nodejs');
}

npm管理modules

npm init //初始化npm管理,生产package.json
{
"name": "xh",
"version": "1.0.0",
"description": "",
"main": "modular01.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"backbone": "^1.3.3",
"underscore": "^1.8.3"
}
npm install //如果有package.json 会自动下载dependencies 里面的插件
npm install backbone -S //加上-S,会保存到package.json里面

搭建一个web服务器。

建立一个http.js

var http = require('http');

var server = http.createServer(function(require,response){
console.log('got a require');
response.write('hi');
response.end();
}); server.listen(3000);
//cmd上面输入
node http.js

然后在浏览器里面输入 localhost:3000,就能看到hi

上一篇:javascript 知道这20个正则表达式,能让你少写1,000行代码


下一篇:NGUI使用教程(2) 使用NGUI创建2D场景而且加入标签和button