首页
好物推荐
薅羊毛领红包
好看壁纸
更多
隐私政策
友情链接
时光机
搜索
1
使用 docker 快速安装 Home Assistant
6,125 阅读
2
Ipad mini2 降级到IOS10.3.3系统
4,120 阅读
3
Home Assistant集成OpenWrt
3,553 阅读
4
华为手机开启ADB进行WIFI远程调试
3,487 阅读
5
小米电视开机广告和乐播投屏广告Hosts屏蔽列表
3,291 阅读
无分类
智能家居
心得随想
文档教程
登录
Search
标签搜索
Linux
JS
教程
CSS
HTML
配置
NodeJS
Docker
解决方案
文档
Git
Java
技术培训
Hadoop
Mac
Windows
RiotJS
Python
VPS
Home Assistant
DONG HAO
累计撰写
154
篇文章
累计收到
59
条评论
首页
栏目
无分类
智能家居
心得随想
文档教程
页面
好物推荐
薅羊毛领红包
好看壁纸
隐私政策
友情链接
时光机
搜索到
12
篇与
NodeJS
的结果
2018-06-27
Nodejs下载文件
function downloadFile(_url, _filename, _rewrite, _startFunc, _endFunc, _errorFunc) { if (!_filename) { let us = _url.split('/'); _filename = us[us.length - 1]; } _mkdirsSync(path.join(app.getPath("userData"), 'resources')); let _realpath = path.join(app.getPath("userData"), 'resources', _filename); if (_rewrite) { _download(_url, _realpath); return; } fs.exists(_realpath, (exists) => { if (!exists) _download(_url, _realpath); }) function _download(_url, _realpath) { http.get(_url, (response) => { if (typeof _startFunc === "function") _startFunc(_realpath); response.pipe(fs.createWriteStream(_realpath)); response.on('error', function () { if (typeof _errorFunc === "function") _errorFunc(_realpath); }) response.on('end', function () { let _realpathDir = _realpath.split('.'); _realpathDir.pop(); fs.createReadStream(_realpath).pipe(unzipper.Extract({ path: _realpathDir.join('') })); if (typeof _endFunc === "function") _endFunc(_realpath); }) }) } function _mkdirsSync(dirname, mode) { if (fs.existsSync(dirname)) { return true; } else { if (_mkdirsSync(path.dirname(dirname), mode)) { fs.mkdirSync(dirname, mode); return true; } } } }
2018年06月27日
180 阅读
0 评论
0 点赞
2018-06-27
fs递归删除文件或者文件夹
function deleteRecursive(path) { if (fs.existsSync(path)) { // file if (fs.statSync(path).isFile()) { fs.unlinkSync(path); return; } // directory let files = []; if (fs.statSync(path).isDirectory()) { files = fs.readdirSync(path); files.forEach(function (file, index) { deleteRecursive(path + "/" + file); }); fs.rmdirSync(path); } } };
2018年06月27日
125 阅读
0 评论
0 点赞
2018-05-03
nodejs动态加载文件夹内模块
定义 ./modules/index.jsconst fs = require("fs"); const colors = require("colors"); module.exports = { controller: null, path: '', init: function(path, controller) { if (!controller) { console.error("参数controller未设置"); return false; } this.controller = controller; this.path = path ? path : this.path; this.listDir(this.path); }, listDir: function(dir) { var fileList = fs.readdirSync(dir, 'utf-8'); for (var i = 0; i < fileList.length; i++) { if (fileList[i] == 'index.js') continue; var stat = fs.lstatSync(dir + fileList[i]); if (stat.isDirectory()) { this.listDir(dir + fileList[i] + '/'); } else { this.loadRoute(dir + fileList[i]); } } }, loadRoute: function(routeFile) { let filename = routeFile.substring(routeFile.lastIndexOf('/') + 1); let current_filename = './' + filename.substring(0, filename.lastIndexOf('.')); require(current_filename)(this.controller); console.log(`module [${filename.substring(0, filename.lastIndexOf('.'))}] load success.`.green); }, }; 调用modules.init('./modules/', controller);
2018年05月03日
121 阅读
0 评论
0 点赞
2018-05-03
nodejs控制台彩色文字输出
console.log('\x1B[36m%s\x1B[0m', info); //cyanconsole.log('\x1B[33m%s\x1b[0m:', path); //yellow var styles = { 'bold' : ['\x1B[1m', '\x1B[22m'], 'italic' : ['\x1B[3m', '\x1B[23m'], 'underline' : ['\x1B[4m', '\x1B[24m'], 'inverse' : ['\x1B[7m', '\x1B[27m'], 'strikethrough' : ['\x1B[9m', '\x1B[29m'], 'white' : ['\x1B[37m', '\x1B[39m'], 'grey' : ['\x1B[90m', '\x1B[39m'], 'black' : ['\x1B[30m', '\x1B[39m'], 'blue' : ['\x1B[34m', '\x1B[39m'], 'cyan' : ['\x1B[36m', '\x1B[39m'], 'green' : ['\x1B[32m', '\x1B[39m'], 'magenta' : ['\x1B[35m', '\x1B[39m'], 'red' : ['\x1B[31m', '\x1B[39m'], 'yellow' : ['\x1B[33m', '\x1B[39m'], 'whiteBG' : ['\x1B[47m', '\x1B[49m'], 'greyBG' : ['\x1B[49;5;8m', '\x1B[49m'], 'blackBG' : ['\x1B[40m', '\x1B[49m'], 'blueBG' : ['\x1B[44m', '\x1B[49m'], 'cyanBG' : ['\x1B[46m', '\x1B[49m'], 'greenBG' : ['\x1B[42m', '\x1B[49m'], 'magentaBG' : ['\x1B[45m', '\x1B[49m'], 'redBG' : ['\x1B[41m', '\x1B[49m'], 'yellowBG' : ['\x1B[43m', '\x1B[49m'] };
2018年05月03日
245 阅读
0 评论
0 点赞
2017-11-21
axios异步转同步请求模式
axios是一个优秀的http异步请求库,但是很多时候需要做一些同步请求,于是我封装了下同步的方法axiosPro 函数代码const axios = require('axios') function axiosPro(axiosArgs) { const { method, url, data, params, headers, responseType } = axiosArgs return new Promise(function (resolve, reject) { axios({ method: method, url: url, data: data, params: params, headers: headers, responseType: responseType }).then(function (ret) { resolve(ret); }).catch(function (err) { reject(err); }) }) }; 使用module.exports = async ctx => { const ret = await axiosPro(ctx.request.body) console.log(ret.data)//同步输出结果 ctx.body = ret.data }
2017年11月21日
761 阅读
0 评论
0 点赞
2017-11-21
Nodejs断点续传服务示例
断点续传:从文件已经下载的地方开始继续下载。 在以前版本的 HTTP 协议是不支持断点的,HTTP/1.1 开始就支持了。 一般断点下载时才用到 Range 和 Content-Range 实体头 代码示例http.createServer(function(request, response) { var pathname = url.parse(request.url).pathname; var realpath = path.join(__dirname, '..', path.normalize(pathname.replace(/\.\./g, ""))); var ext = path.extname(realpath); ext = ext ? ext.slice(1) : "unknown"; var contentType = mime[ext].contentType; fs.exists(realpath, function(exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("This request URL " + pathname + "was not found on this server"); response.end(); } else { response.setHeader("Content-Type", contentType); var stats = fs.statSync(realpath); if (request.headers["range"]) { var range = parseRange(request.headers["range"], stats.size); if (range) { response.setHeader("Content-Range", "bytes " + range.start + "-" + range.end + "/" + stats.size); response.setHeader("Content-Length", (range.end - range.start + 1)); var stream = fs.createReadStream(realpath, { "start": range.start, "end": range.end }); response.writeHead('206', "Partial Content"); stream.pipe(response); } else { response.removeHeader("Content-Length"); response.writeHead(416, "Request Range Not Satisfiable"); response.end(); } } else { var stream = fs.createReadStream(realpath); response.writeHead('200', "Partial Content"); stream.pipe(response); } } }); }).listen(8080) parseRange 函数function parseRange(str, size) { if (str.indexOf(",") != -1) { return; } if (str.indexOf("=") != -1) { var pos = str.indexOf("=") var str = str.substr(6, str.length) } var range = str.split("-"); var start = parseInt(range[0], 10) var end = parseInt(range[1], 10) || size - 1 // Case: -100 if (isNaN(start)) { start = size - end; end = size - 1; // Case: 100- } else if (isNaN(end)) { end = size - 1; } // Invalid if (isNaN(start) || isNaN(end) || start > end || end > size) { return; } return { start: start, end: end }; }; mime{ "css": { "contentType": "text/css" }, "gif": { "contentType": "image/gif", "mediaType": "image" }, "html": { "contentType": "text/html" }, "ico": { "contentType": "image/x-icon", "mediaType": "image" }, "jpeg": { "contentType": "image/jpeg", "mediaType": "image" }, "jpg": { "contentType": "image/jpeg", "mediaType": "image" }, "js": { "contentType": "text/javascript" }, "json": { "contentType": "application/json" }, "pdf": { "contentType": "application/pdf" }, "png": { "contentType": "image/png", "mediaType": "image" }, "svg": { "contentType": "image/svg+xml", "mediaType": "image" }, "swf": { "contentType": "application/x-shockwave-flash" }, "tiff": { "contentType": "image/tiff" }, "txt": { "contentType": "text/plain" }, "wav": { "contentType": "audio/x-wav" }, "wma": { "contentType": "audio/x-ms-wma" }, "wmv": { "contentType": "video/x-ms-wmv" }, "xml": { "contentType": "text/xml" }, "mp4": { "contentType": "video/mp4", "mediaType": "video" }, "m3u8": { "contentType": "application/x-mpegURL", "mediaType": "video" }, "unkown": { "contentType": "application/octet-stream" } }
2017年11月21日
208 阅读
0 评论
0 点赞
2017-06-23
如何在windows下升级node & npm
upgrade nodenode有一个模块叫n,是专门用来管理node.js的版本的。 第一步:安装n模块: npm install -g n第二步: 升级node.js到最新稳定版 n stableBut,这在我的windows环境下报错: Google了一圈: (敲黑板!!)As stated in the readme file of that module, the module does not support Windows, hence the error you are getting. If you would like to update Node.js on Windows you can find binaries at https://nodejs.org/en/download/current/.于是乖乖去nodejs网站上下载.msi并安装,command line下成功,顺带着npm也一并更新了:
2017年06月23日
345 阅读
0 评论
0 点赞
2017-06-09
ghost使用pm2管理启动
以前运行ghost使用forever管理启动,但是现在发现pm2管理nodejs程序更好,遂尝试改造代码如下pm2.json{ "apps": [{ "name": "ghost", "script": "index.js", "cwd": "/data/wwwroot/blog.minws.com", "exec_mode": "cluster", "instances": 0, "max_memory_restart": "256M", "autorestart": true, "node_args": [], "args": [], "env": { "NODE_ENV": "production" } }] } 启动pm2 start pm2.json<br/>
2017年06月09日
253 阅读
0 评论
0 点赞
2017-05-14
electron安装sqlite3
npm install sqlite3 --savenpm install electron-rebuild --save npm install electron-prebuilt --save ###貌似无效 .\node_modules\.bin\electron-rebuild.cmd ###手动编译 cd .\node_modules\sqlite3 npm install nan --save npm run prepublish node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.4-win32-x64 ###target是electron -v的版本 node-gyp rebuild --target=1.6.7 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/electron/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.4-win32-x64
2017年05月14日
100 阅读
0 评论
0 点赞
2017-05-14
MSB4132错误解决
错误信息:npm编译报错如下:error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "4.0"或者MSBUILD : error MSB4132: 无法识别工具版本“2.0”。可用的工具版本为 "4.0"。解决方案:在管理员命令行工具下输入npm install --global --production windows-build-tools npm config set msvs_version 2015 --global 重新打开命令行
2017年05月14日
340 阅读
0 评论
0 点赞
2017-04-11
Sqlite的js编辑库-sql.js
npm安装npm install sql.jsnpm地址https://www.npmjs.com/package/sql.jsgithub&文档地址Github Document示例const path = require('path') const fs = require('fs'); const sql = require('sql.js'); function getPath() { let pathArr = Array.prototype.slice.call(arguments); pathArr.unshift(__dirname); return path.join(pathArr.join('/')); } /** SQL **/ function sql(path) { this.path = path; this.db; } sql.prototype = { init: function() { let filebuffer = fs.readFileSync(this.path); let database = new sqljs.Database(filebuffer); this.db = database; }, find: function(_sql, _params) { let stmt = this.db.prepare(_sql), res = []; stmt.bind(_params); while (stmt.step()) { res.push(stmt.getAsObject()); } stmt.free(); return res; }, findOne: function(_sql, _params) { return this.find(_sql, _params)[0]; }, run: function(_sql, _params) { return this.db.run(_sql, _params);; } }; (function() { const SQL = new sql(getPath('data', 'app.sqlite')); SQL.init(); let res = SQL.find("select * from user"); console.log(res); })();
2017年04月11日
144 阅读
0 评论
0 点赞
2016-10-25
用forever运行ghost博客
如果没有安装,先安装npm install forever -g 查看forever运行状态forever list 运行GhostNODE_ENV=production forever start index.js 关闭Ghostforever stop index.js
2016年10月25日
80 阅读
0 评论
0 点赞