Skip to content

Instantly share code, notes, and snippets.

View lastday154's full-sized avatar

Steve Hoang lastday154

  • NIT Warangal, India
View GitHub Profile
@lastday154
lastday154 / async.js
Created June 4, 2018 14:15
async/await loops
'use strict';
const delay = () => new Promise((resolve) => setTimeout(resolve, 300));
const delayedLog = async (item) => {
await delay();
console.log(item);
};
const processArray = (array) => {
@lastday154
lastday154 / debug_lambda.js
Created June 1, 2018 04:20
debug lambda vscode
- package.json
"scripts": {
"start": "./node_modules/.bin/serverless offline -s dev",
"debug": "export SLS_DEBUG=* && node --inspect ./node_modules/.bin/serverless offline -s dev",
"test": "mocha"
},
- serverless.yml
plugins:
- serverless-offline
@lastday154
lastday154 / elasticsearch.js
Created May 28, 2018 04:39
unit test with sinon, mochai, chai, supertest
const app = require('../../app');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const ElasticSearch = require('../../models/elasticsearch');
const url = '/api/search?q=money&page_id=333&lang=en';
describe('GET /api/search', () => {
let sandbox;
@lastday154
lastday154 / ifelse.js
Created May 25, 2018 03:22
if-else flow in promise (bluebird)
(conditionA
? fs.writeFileAsync(file, jsonData)
: Promise.resolve())
.then(functionA);
var waitFor;
if (conditionA)
waitFor = fs.writeFileAsync(file, jsonData);
else
waitFor = Promise.resolve(undefined); // wait for nothing,
@lastday154
lastday154 / ifelse.js
Created May 25, 2018 03:22
if-else flow in promise (bluebird)
(conditionA
? fs.writeFileAsync(file, jsonData)
: Promise.resolve())
.then(functionA);
var waitFor;
if (conditionA)
waitFor = fs.writeFileAsync(file, jsonData);
else
waitFor = Promise.resolve(undefined); // wait for nothing,
@lastday154
lastday154 / setting.js
Created May 17, 2018 07:19
elasticsearch mapping
POST movies
{
"settings": {
"index": {
"analysis": {
"filter": {},
"analyzer": {
"keyword_analyzer": {
"filter": [
"lowercase",
@lastday154
lastday154 / upload.js
Created May 17, 2018 02:59
upload file nodejs
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
const oldPath = files.file.path;
const newPath = path.join(process.cwd(), '/uploads/', files.file.name);
fs.rename(oldPath, newPath, (errRn) => {
if (errRn) {
res.status(500);
res.json({ success: false });
return;
}
{
"extends": "airbnb-base",
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"rules": {
"max-len": [
@lastday154
lastday154 / upload_demo_html.html
Created May 15, 2018 08:49 — forked from paambaati/upload_demo_html.html
Uploading files using NodeJS and Express 4
<html>
<body>
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="text" name="title">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
https://balsamiq.com/