其实主要想写一个图床网站的上传方法,顺便记录一次 npm 包的开发。javascript
mkdir upload-img && cd upload-img npm init
而后按提示输入 package name
、version
、 description
等信息就能够了。
初始化完会在当前目录生成一个 package.json
文件,熟悉 现代PHP
的应该都知道,这个文件跟 composer.json
的做用是同样的,都用于描述这个项目的各项信息,包括依赖和脚本等。java
新建 index.js
文件,开始编码。node
默认的入口文件是 index.js ,固然你能够在
package.json
随意更改入口文件。ios
/*! Copyright (c) 2017 96qbhy. Licensed under the MIT License (MIT) */ /* global define */ (function () { 'use strict'; var axios = window.axios || require('axios'); function uploadImg(file) { var data = new FormData(); data.append('smfile', file); data.append('ssl', true); return axios.post('https://sm.ms/api/upload', data).then(data => data.data); } if (typeof module !== 'undefined' && module.exports) { module.exports = uploadImg; } else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { // register as 'uploadImg', consistent with npm package name define('uploadImg', [], function () { return uploadImg; }); } else { window.uploadImg = uploadImg; } }());
而后基本套路走一发, 代码就那么多。git
实际代码已更新,最新的代码请移步该项目的 github仓库。
做为一个开源项目,一个好的readme.md
必不可少,此处我省略了readme.md
的书写,由于太多了。github
npm adduser
,按提示输入 username 和 password 以及 email。npm publish
。OK,装逼完毕,这样就完成了一个简单的 npm
包的开发和发布。npm
github
仓库github
建立仓库。git init
,添加 .gititnore
文件,排除须要排除的文件,例如 node_modules
和 .idea
等。git remote add origin {仓库地址}
该项目我托管在
github
, 地址是 https://github.com/96qbhy/smms
sm.ms 是一款免费的图床网站,smms
是sm.ms
的 js 上传插件 。json