Skip to content

Instantly share code, notes, and snippets.

View cuipengfei's full-sized avatar
:octocat:

崔鹏飞 Cui Pengfei cuipengfei

:octocat:
View GitHub Profile
@cuipengfei
cuipengfei / filterlist.js
Last active December 31, 2015 03:39
learn you node filter list of file names
var fs = require('fs');
String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var dir = process.argv[2];
var postFix = process.argv[3];
fs.readdir(dir, function (err, list) {
@cuipengfei
cuipengfei / caller.js
Last active December 31, 2015 03:39
MAKE IT MODULAR
var filterListModule = require('./filterListModule');
var dir = process.argv[2];
var postFix = process.argv[3];
filterListModule(dir, postFix, function (err, filteredList) {
filteredList.forEach(function (fileName) {
console.log(fileName);
});
});
@cuipengfei
cuipengfei / learnyounodehttpget.js
Created December 13, 2013 05:34
learn you node http get
var http = require('http')
var url = process.argv[2]
http.get(url, function (resp) {
resp.setEncoding('utf8')
resp.on("data", console.log)
});
@cuipengfei
cuipengfei / httpCollect.js
Created December 13, 2013 10:15
learn you node http collect
var http = require('http')
var url = process.argv[2]
http.get(url, function (resp) {
var respContent = ""
resp.setEncoding('utf8')
resp.on("data",function (data) {
respContent += data
@cuipengfei
cuipengfei / timeserver.js
Created December 14, 2013 05:52
learn you node time server
var net = require('net')
var moment = require("moment")
var port = process.argv[2]
net.createServer(function (socket) {
socket.end(moment().format("YYYY-MM-DD HH:mm") + "\n")
}).listen(port)
@cuipengfei
cuipengfei / httpfileserver.js
Created December 14, 2013 06:17
learn you node http file server
var http = require('http')
var fs = require('fs')
var port = process.argv[2]
var fileName = process.argv[3]
http.createServer(function (request, response) {
fs.createReadStream(fileName).pipe(response)
}).listen(port)
@cuipengfei
cuipengfei / httpuppercase.js
Created December 14, 2013 06:34
learn you node http uppercase
var http = require('http')
var map = require('through2-map')
var port = process.argv[2]
http.createServer(function (request, response) {
request
.pipe(map(function (chunk) {
return chunk.toString().toUpperCase()
}))
@cuipengfei
cuipengfei / httpJsonAPI.js
Created December 14, 2013 07:13
learn you node http json api
var http = require('http')
var url = require('url')
var port = process.argv[2]
http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'application/json' })
var reqContent = url.parse(request.url, true)
@cuipengfei
cuipengfei / jade.js
Created December 14, 2013 10:04
express work jade
var express = require('express')
var app = express()
app.set('view engine', 'jade')
app.set('views', process.argv[3])
app.get('/home', function (req, res) {
res.render('index', {date: new Date().toDateString()})
})
app.listen(process.argv[2])
@cuipengfei
cuipengfei / goodOldForm.js
Created December 14, 2013 10:09
express work good old form
var express = require('express')
var app = express()
app.use(express.urlencoded())
app.post('/form', function (req, res) {
var formDataReversed = req.body.str.split('').reverse().join('')
res.end(formDataReversed)
})
app.listen(process.argv[2])