Skip to content

Instantly share code, notes, and snippets.

View amandeepmittal's full-sized avatar

Aman Mittal amandeepmittal

View GitHub Profile
// RETWEET BOT ==========================
// find latest tweet according the query 'q' in params
var retweet = function() {
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
// for more parametes, see: https://dev.twitter.com/rest/reference/get/search/tweets
// FAVORITE BOT====================
// find a random tweet and 'favorite' it
var favoriteTweet = function(){
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
// for more parametes, see: https://dev.twitter.com/rest/reference
// Dependencies =========================
var
twit = require('twit'),
config = require('./config');
var Twitter = new twit(config);
// RETWEET BOT ==========================
// Use Streams API for interacting with a USER ==========
// set up a user stream
var stream = Twitter.stream('user');
// FOLLOW-Reply BOT ===========================
// when someone follows
stream.on('follow', followed);

NODEJS Overview

What is Nodejs?

  • Platform built on Chrome's JavaScript runtime, for building fast, scalable network applications
  • Nodejs uses event driven, non-blocking I/O

NOTE

@amandeepmittal
amandeepmittal / top-npm.md
Created March 17, 2017 15:55
Top Npm Commands
  • npm init: displays a simple wizard to help you create and describe your new project;
  • npm install module_name: install a module;
  • fnpm install -g module_name: install a global module;
  • npm install module_name --save: install a module and add it into the package.json file, inside dependencies;
  • npm install module_name --save-dev: install a module and add it into the package.json file, inside devDependencies;
  • npm list: lists all the modules installed on the project;
  • npm list -g: lists all the global modules installed on the OS;
  • npm remove module_name: uninstall a module from the project;
  • npm remove -g module_name: uninstall a global module;
  • npm remove module_name --save: uninstall a module from the project and also remove it from the attribute dependencies;
@amandeepmittal
amandeepmittal / user.js
Created March 31, 2017 16:16
faker gist user.js
const faker = require('faker')
const User = {
name: faker.name.findName(),
email: faker.internet.email(),
website: faker.internet.url(),
address: faker.address.streetAddress() + faker.address.city() + faker.address.country(),
bio: faker.lorem.sentences(),
image: faker.image.avatar()
}
@amandeepmittal
amandeepmittal / error-handler.js
Created April 22, 2017 12:50
Express Handlers
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.send('error', {
message: err.message,
error: err
});
}); }
'use strict';
module.exports = function(sequelize, DataTypes) {
var user = sequelize.define('user', {
id:{
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
username: DataTypes.INTEGER,
router.get('/:page', (req, res) => {
let limit = 50; // number of records per page
let offset = 0;
db.user.findAndCountAll()
.then((data) => {
let page = req.params.page; // page number
let pages = Math.ceil(data.count / limit);
offset = limit * (page - 1);
db.user.findAll({
attributes: ['id', 'first_name', 'last_name', 'date_of_birth'],