用Node.js创建一个静态服务器,然后将项目部署在这个服务器中
源代码:
var http = require( 'http' );
var port = 8080 ;
var hostname = '127.0.0.1'
var path = require( 'path' ) // 磁盘路径处理的模块
var fs = require( 'fs' ) // 操作文件、目录的模块
var url = require( 'url' ) // 处理url
var server = http.createServer( function ( request, response ) {
var staticPath = path.join( __dirname,'static' )
var urlObj = url.parse( request.url )
/* Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/static/css/index.css',
path: '/static/css/index.css',
href: '/static/css/index.css' }
*/
if( urlObj.pathname === '/'){
urlObj.pathname += 'index.html'
}
console.log( urlObj.pathname )
// fs.readFile(路径,字符编码,错误优先的回调函数)
var filePath = path.join( staticPath, urlObj.pathname )
fs.readFile( filePath,'binary',function( error,filecontent ) {
if ( error ){
throw error
}else {
response.write( filecontent, 'binary' )
response.end()
}
})
})
server.listen( port , hostname , function () {
console.log( `The server is runing: http://${ hostname }:${ port }` )
})
模块/包 与Common.js
Node.js使用了Common.js规范
Node.js中Common.js规范有三种使用类型:
- 内置模块 path url fs precess
内置模块源代码:
内置模块很多,我们下面以 磁盘处理的模块 为例
内置模块我们注重的模块的使用
使用步骤:
1. 先引入模块,然后用一个变量保存这个模块( 内置模块在requrie(模块名称) )
2. 使用这个模块上的api( 方法 ) 【常用】
path.join
path.resolve
以上两个api,将一个目录的名称拼接到一个磁盘路径上
// {
// resolve: [Function: resolve],
// normalize: [Function: normalize],
// isAbsolute: [Function: isAbsolute],
// join: [Function: join],
// relative: [Function: relative],
// toNamespacedPath: [Function: toNamespacedPath],
// dirname: [Function: dirname],
// basename: [Function: basename],
// extname: [Function: extname],
// format: [Function: format],
// parse: [Function: parse],
// sep: '\\',
// delimiter: ';',
// win32: [Circular],
// posix:
// { resolve: [Function: resolve],
// normalize: [Function: normalize],
// isAbsolute: [Function: isAbsolute],
// join: [Function: join],
// relative: [Function: relative],
// toNamespacedPath: [Function: toNamespacedPath],
// dirname: [Function: dirname],
// basename: [Function: basename],
// extname: [Function: extname],
// format: [Function: format],
// parse: [Function: parse],
// sep: '/',
// delimiter: ':',
// win32: [Circular],
// posix: [Circular],
// _makeLong: [Function: toNamespacedPath] },
// _makeLong: [Function: toNamespacedPath] }
var path = require( 'path' )
var process = require( 'process' )
// console.log( process )
/* // path.resolve
console.log( __dirname )//当前文件lujing
console.log( path.resolve( __dirname,'../static') )
console.log( path.join( __dirname,'../static') )
二者效果相同
*/
- 第三方模块:插件
前端的第三方模块基本都存放在 http://npmjs.com
npmjs.com中会第三方包的 文档 就在这里
文档阅读能力
以一个 request 的一个模块来进行研究
需要一个package.json的文件来记录我们安装过的模块
package.json 文件的创建
-
npm init 一步一步进行
-
快速创建 npm init -y / cnpm init -y / yarn init -y
命令安装的环境区分
-
cnpm i request --save 生产环境安装
简写: cnpm i request -S生产环境安装在package.json中记录在 dependencies 中
-
cnpm i request --save-dev 开发环境安装
简写: cnpm i request -D开发环境安装在 package.json中记录在 depenDencies 中
后端 不存在跨域问题, 后端是不依靠浏览器,
第三方的包使用步骤:
- 引用 ( 第三方包如果使用了模块化安装, require( 模块名称 ) )
- 使用:
举例: request是用来数据请求的 : 接下来我们去请求一下豆瓣的数据
var request = require( 'request' )
var http = require ( 'http' )
http.createServer( function ( req,res ) {
// request ( url, function ( error, response, body ) {})
request ( 'https://m.lagou.com/listmore.json', function ( error, response, body ) {
if( error ) throw error
// console.log( response ) //请求的接口的信息
// console.log( body ) // 请求回来的数据
res.write( body )
res.end( )
})
}).listen( 8001 )
- 自定义模块
自定义模块步骤:-
先定义一个 变量, 变量值可以随意
-
导出模块
module.exports = 变量名
module.exports = {
变量名
} -
导入
如果是第一种方式导出 , var 变量名 = require( 相对路径 )如果是第二种方式导出, var { 变量名 } = require ( 相对路径 )
var {name} = require( './name.js' ) var student = { id: 1, name: '老彭' } // module.exports = student console.log( name ) module.exports = { student, name }
-
前端模块化
- AMD ( require.js )
- CMD ( sea.js )
- Common.js ( module.export require )
- es6 模块化 ( export import )
终端
- gitbash 客户端软件中的命令 叫 linux命令
- clear 清屏
- ls 列出当前目录下的所有目录和文件
- cmd中的命令叫做 DOS命令
- dir/w 列出当前目录下的所有的目录和文件
- cls 清屏
自定义模块上传 npmjs.com 过程
-
创建package.json
-
在 http://npmjs.com 上注册账号
-
激活账号( npmjs.com会发送一个邮件给你的注册邮箱 )
-
使用命令登录npmjs.com ( 登录前将你的源从淘宝源 切到 npmjs)
- 问题 : 如何切换源呢?
- 解决: 使用 nrm 切换
- nrm 安装
$ npm i nrm -g
$ npm adduser
-
如果登录成功, 提示为: Logged in as yanyabing on https://registry.npmjs.org/.
-
发布包到npmjs
$ npm publish
-
在npmjs官网查看包有没有发送上去
-
下载包来使用一下
$ npm i 包名称 -S / -D