Saturday, 1 April 2017

Grunt browser-sync task along with copy and watch

I have a few questions for implementing browser-sync with Grunt in a NodeJS app.

I'm running a local Node server and have a working directory called 'client' that's located in the root of my project directory. This contains all my HTML/CSS/JS files. Grunt is watching these for changes and copying them over to a 'public' directory in my server folder. Am I missing something in my Gruntfile (or maybe my other files) that is preventing me from using browser-sync?

My Node server is running on port 5000. The app loads fine if I navigate to localhost:5000, but when I run 'grunt' in a separate terminal window it automatically opens a browser with a request for localhost:3000, which results in "Cannot GET /".

Do I need to manually put this script at the end of my HTML or is Grunt supposed to automatically inject this?

<script id="__bs_script__">//<![CDATA[document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.js?v=2.18.8'><\/script>".replace("HOST", location.hostname));//]]></script>

Additional questions commented in my Gruntfile below. I'm pretty new to task-running, so apologies if this is an easy fix.

module.exports = function(grunt) {
  grunt.initConfig({
      pkg: grunt.file.readJSON('package.json'),
      uglify: {
          build: {
              src: ['client/scripts/client.js'],
              dest: 'server/public/scripts/client.min.js'
          }
      },
      copy: {
          jquery: {
              expand: true,
              cwd: 'node_modules/jquery/dist/',
              src: ['jquery.js'],
              dest: 'server/public/vendors/'
          },
          bootstrap: {
              expand: true,
              cwd: 'node_modules/bootstrap/dist/css/',
              src: ['bootstrap.css'],
              dest: 'server/public/vendors/'
          },
          styles: {
              expand: true,
              cwd: 'client/stylesheets',
              src: 'styles.css',
              dest: 'server/public/stylesheets',
          },
          html: {
              expand: true,
              cwd: 'client/views',
              src: ['index.html'],
              dest: 'server/public/views'
          }
      },
      watch: {
          files: ['client/scripts/*.js', 'client/stylesheets/*.css', 'client/views/*.html'],
          tasks: ['uglify', 'copy']
      },
      browserSync: { // QUESTION REGARDING THIS TASK
        dev: {
          bsFiles: { // SHOULD THESE BE THE FILES IN MY WORKING DIR?
              src : ['server/public/stylesheets/*.css',
                     'server/public/scripts/*.js',
                     'server/public/views/*.html']
          },
          options: { // IS THIS SUB-TASK NEEDED?
            watchTask: true,
            server: './client' // WORKING DIRECTORY? OR PUBLIC DIR?
          }
        }
      }
  });

  grunt.loadNpmTasks('grunt-browser-sync');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('default', ['browserSync', 'uglify', 'copy', 'watch']);
};


via Dan Zera

ExpressJS - How to change the value of key in the url of a get request

So I am developing custom pagination functionality with express and mongoose. I have a get parameter called page and I need to change the value for every different page. The problem is that the way I do it it keeps appending "page" key-value pairs to the url like this "&page=1&page=1&page=1". That is because I don't simply change the value of "page" but I append the key-value pair each time like so:

currentPageUrl: req.originalUrl + '&page=' + currentPage

I need just to change the value of the "page" key. How can I accomplish this? Any advice, express helper function or custom solution would help a lot. Thanks in advance!



via Codearts

dynamic rendering in express using loop won't work

I have bunch of static file, they are named article1, article2 and so on. So I know it worked when I do

app.get('/this-is-my-article-routes', (req, res) => {
        res.render('article1');
});

but it's too tedious and has lots of repeated code. I tried this but it doesn't work?

const articleArr = [
    'this-is-my-article-routes'
];

for (i = 0; i<9; i++) {
    app.get(`/${articleArr[i]}`, (req, res) => {
        res.render(`article${i}`);
    });
}

Is this even possible? or there's something wrong with my codes?



via Jenny Mok

using influxdb nodejs library in reactjs

I'm in the processes of switching out elasticsearch in a reactjs app with influxdb and am trying to use the influxdb node-js library (https://node-influx.github.io/class/src/index.js~InfluxDB.html) but am having some issues running a query.

My previous code looked like this which works(this action gets dispatch once the store is initialized):

export function loadHosts() {

    return function(dispatch) {
        console.log("elastic");
        return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "sort" : { "kafka_topic" : {"order" : "desc"}},
                    "aggs" : {
                        "hosts" : {
                            "terms" : {
                              "field" : "host",
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-60s"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(hosts => {

                hosts = {host_selected: "Select Host", hosts}
                dispatch(loadHostsSuccess(hosts));

        }).catch(error => {
            throw(error);
        });
    };

}

and then I added in another promise just to get the influxdata and to just test to see if the call would actually happen...here is how I modified it:

export function loadHosts() {

    return function(dispatch) {
        console.log("elastic");
        return eSclient.search({

            index: "sdpjmx",
            type: "kafkajmx",
            body:  {
                    "size": 0,
                    "sort" : { "kafka_topic" : {"order" : "desc"}},
                    "aggs" : {
                        "hosts" : {
                            "terms" : {
                              "field" : "host",
                              "size": 500
                            }
                        }
                    },  
                    "query": {
                    "bool": {
                      "filter": [
                        {
                          "range": {
                            "@timestamp": {
                              "gte": "now-60s"
                            }
                          }
                        }
                        ]
                      }
                    }
                }

        }).then(hosts => {

            return influx.query(

                'select distinct(host) from jmx where time > now() - 180m'

            ).then(infltest => {            


                hosts = {host_selected: "Select Host", hosts}
                dispatch(loadHostsSuccess(hosts));

            }).catch(err => {
                throw(err);
            });

        }).catch(error => {
            throw(error);
        });
    };

}

When I do this, react-redux fails to send a state so the components are failing, so it looks like I'm not using the influxdb library right but I'm not able to figure out what I'm doing wrong.

I import the library and create the connection like this:

import * as Influx from 'influx';

const influx = new Influx.InfluxDB({
    host: "http://10.111.11.88:9050",
    database: "sdp_metrics"
})



via user2061886

gulp + mocha + typescript unit test

I have an error when I create new instance on test file. This is my test:

    /// <reference path="../typings/globals/mocha/index.d.ts" />

import Person from '../src/person/person';

describe('Person', () => {

    let person: Person;

    beforeEach(() => {
        person = new Person();

    });

    describe('getName', () => {

        it('return name', () => {

        });
    });
});

And my gulp task:

var gulp = require("gulp");
var ts = require("gulp-typescript");
var mocha = require("gulp-mocha");
var tsProject = ts.createProject("./tsconfig.json");

gulp.task('watch', function () {
    gulp.watch('./src/**/**/*.ts', ['ts']);
});

gulp.task('ts', function () {
    return tsProject.src()
        .pipe(tsProject())
        .js.pipe(gulp.dest("dist"));
});

gulp.task('test', function () {

    return gulp.src('./tests/*.spec.ts',
        {
            base: '.'
        })
        /*transpile*/
        .pipe(tsProject())
        /*flush to disk*/
        .pipe(gulp.dest('.'))
        /*execute tests*/
        .pipe(mocha())
        .on("error", function(err) {
            console.log(err)
        });
});

gulp.task("default", ['watch', 'ts']);

So, when I launch the test an error occured, but if I comment person = new Person() then everything works.

Somebody knows what I am doing wrong?



via Gustavo Bueno

for loop comparing two objects not working properly in javascript

I have a snippet of code that is not working as expected...

for(let a in html){

    // check if first time (empty json)
    if(JSON.stringify(dbdata) === '{}'){
        console.log('new id found', a);
        newIdsFound[a] = html[a];
        dbdata[a] = html[a];
    }

    for(let b in dbdata){

        if(a == b){
            console.log('id exists, skipping...', a);
        }else{
            console.log('new id found', a);
            newIdsFound[a] = html[a];
            dbdata[a] = html[a];
        }

    }
}

but this is what I get in the console:

id exists, skipping... 9912
new id found 9912
new id found 9912
new id found 9912
new id found 9912
new id found 9912
new id found 10077
id exists, skipping... 10077
new id found 10077
new id found 10077
new id found 10077
new id found 10077
new id found 10188
new id found 10188
id exists, skipping... 10188
new id found 10188
new id found 10188
new id found 10188
new id found 10267
new id found 10267
new id found 10267
id exists, skipping... 10267
new id found 10267
new id found 10267
new id found 10271
new id found 10271
new id found 10271
new id found 10271
id exists, skipping... 10271
new id found 10271
new id found 10309
new id found 10309
new id found 10309
new id found 10309
new id found 10309
id exists, skipping... 10309

I am running this code while the json dbdata is still empty but I cannot make it properly working.

After a first population it should skip existing entries but I am doing something wrong..

the code is intended to being executed more times in the future... if there aren't new entries it should skip to make additions, at least it is my ideal behaviour

I am keeping separated objects (dbdata and newIdsFound) so newIdsFound can keep track of the new additions only


Edit: this is an example input of the data I have:

"10271":"<a href=\"http://blabla.com?q=10271\">texthere.\n</a>",
"99999":"<a href=\"http://blabla.com?q=99999\">othertxt.\n</a>",
etc...



via neoDev

Ajax not working with Node js - Express js

Im trying to send data from my server side to my client side with ajax but when i use res.send(data) in node, it show me a empty html with the data and i want to get data from my client side without reloading the page and edit something in my actual html

Here's my server-side code

    app.post('/',(req,res)=>{
    var userName = req.body.username;
    var password = req.body.password;

    connection.query('SELECT * from admin',(err, rows, fields)=> {
      if (!err){
        if((userName == rows[0].usuario) && (password == rows[0].contraseƱa)){
            res.sendStatus(200);    

        }else{
            res.send('Incorrect username or password')
        }
      }else{
        res.send('Error while performing Query.');
      }

    });

});

Here's my client-side code

$(document).ready(function(){
$('#login-form').submit(function (e) {
        setTimeout(function () {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/',
            dataType: 'jsonp',
            data: ({username: $('input[name=username]'),password: $('input[name=password]')}),
            success: function(data,textStatus){
                if(textStatus.statusCode == 200) {
                    $('.error-user').html('Succes');    
                }else{
                    $('.error-user').html('Not Success');
                }


            },
        });
        }, 1000);

});       
});



via Albeiro Espitia Sierra

Mongoose Model Methods: Properties don't save?

I'm trying to write a method on a mongoose model that will populate some fields based on an object I pass in.

let mySchema = mongoose.Schema({
    name: String,
    age: Number,
    street: { type: String, default: 'No' }
});

mySchema.methods.populate = function(o) {
    this.age = o.age + 10;
});

Elsewhere, i'll initialize an instance and run the method:

let newThing = new MySchema();
newThing.populate({ age: 12 });
newThing.save();

This successfully saves a new object in mongo with no properties other than the default street name. Am I doing something wrong?



via opticon

MongoError: E11000 duplicate key error

i'm making a simple blog app using nodejs + express, i can add first post without a problem but when i try to add second post i got his error { MongoError: E11000 duplicate key error collection: restful_blog_app_v2.blogs index: username_1 dup key: { : null }

this is my schema

var BlogSchema = new mongoose.Schema({
    title: String,
    image: String,
    body: String,
    created: {
        type: Date,
        default: Date.now
    },
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    }
});

this is the create new post route

app.post("/blogs", isLoggedIn, function (req, res) {
    req.body.blog.body = req.sanitize(req.body.blog.body);
    var title = req.body.blog.title;
    var image = req.body.blog.image
    var body = req.body.blog.body;
    var created  = req.body.blog.created;
    var author = {
        id: req.user._id,
        username: req.user.username
    }
    var newPost = {
        title: title,
        image: image,
        body: body,
        created: created,
        author: author
    }
    Blog.create(newPost, function (err, newBlog) {
        if (err) {
            console.log(err);
            res.render("new");
        } else {
            console.log(newBlog);
            res.redirect("/blogs");
        }
    });

});

I've tried to dropped the entire database using db.dropDatabase() from the mongo console but the problem still persist, not sure what to do now



via Michael

What is the sleep time between ticks in nodejs

I am asking about the nodejs internals: I would like to know if there there are any sleep time between ticks in nodejs' event loop.

In other words, I assume nodejs internal looks like the code below, I would like to what is the value of sometime, if any.

while(true) {
  for(event in queue) handleEvent(event);
  sleep(sometime);
}

I made such assumption because I believe there must exist some kind of sleeping such that the while loop will not exhaust the CPU.



via KevinResoL

Fast USPS estimates

I'm currently using the easypost library for node, to get shipping estimates for usps and sometimes ups. However, loading these estimates can take awhile (>2secs) which while loading up the cart page for customers can be a long load in.

So In short: I need to find a way to get faster usps estimates (sub 1 sec requests).



via John Dews

How can I prevent data loss when saving web socket messages to mysql?

I am trying to implement a websocket realtime chat system in nodejs. I already have a login system in place and the websocket chat system. The only thing I'm suck on is saving the messages to MySQL so that the user can access their chat history. The problem is that I don't want to send a web socket message to the recipient then have the MySQL insert fail for whatever reason.

A possible solution: The obvious solution is to save the message data in MySQL then send the web socket message to the recipient. If there is an error, send that instead of the message to let the user know. Wouldn't this defeat the purpose of web sockets though, if the server has to wait for a MySQL insert success confirmation?



via LearnSomeMore

Nodejs + Mongoose custom validator to check if unique, if so, update existing record

I'm connecting to an API and copying some of the result data into my own database to be manipulated afterward. Basically I query the API and search for "restaurants", and it brings me a list of restaurants, which I copy some relevant info, such as the unique ID of the restaurant. Let's say in this case one of the restaurants was "Bubba's Bar & Grill". So I insert this and many other restaurants into my own MongoDB database.

Then I connect to the same API and run a search for "bars" and somewhere in this list it will have "Bubba's Bar & Grill" again. Since I want to keep unique records in my own database, what I'd like to do is have a field as type Array that will contain the types of places a single record will be. So in this case the object in MongoDB will look something like this:

{
  name: 'Bubba\'s Bar & Grill',
  id: 'bubbas-bar-and-grill',
  type: ['bar', 'restaurant']
}

I believe the best way to do this is to have some type of custom mongoose validator, or a .pre('save') function to make this happen. I'd tried several ways but unfortunately the MEAN stack is not my primary proficiency as a software developer. Anyone have any ideas for the best way to approach this issue?

Here's my code so far:

server.js

var mongoose    = require('mongoose'),
    api         = require('myapi')

// API Credentials
var id     = 'myapi-id'
var secret = 'myapi-secret'

// Connect to MongoDB
mongoose.Promise = global.Promise
mongoose.connect('mongodb://127.0.0.1/db')
  .then(() =>  console.log('connection succesful'))
  .catch((err) => console.error(err))

// Import Models
var Location = require('./model.places.js')

//Get API Token
api.call('API', 'getAccessToken', {
  'appId': id,
  'appSecret': secret
}).on('success', (token) => {
  var access_token = token.access_token
  console.log('Access token: '+ access_token)
  // Populate Database with Places
  api.call('API', 'getBusinesses', {
    'accessToken': access_token,
    'term': 'restaurant'
  }).on('success', (payload) => {
    for (var i = 0; i < payload.businesses.length; i++) {
      var location = new Location({
        name: payload.businesses[i].name,
        id: payload.businesses[i].id
        type: 'restaurant'
      })
      location.save(function (err) {
        if (err) { console.log(err) }
      })
    }
  })
})

model.places.js

var mongoose = require('mongoose')

var Place = new mongoose.Schema({
  name: { type: String, required: true, trim: true },
  id: { type: String, required: true, trim: true, unique: true }
  type: { type: Array, default: [] }
})

module.exports = mongoose.model('places', PlaceSchema)



via robotsushi

socket.io infinite Client connection in unity

I got Socket.io in unity asset and tried version check. There was no problem. So, I make the new function. If the client_version is not equal to server's current_function,

    socket.on('Version Req', function(version)
    {
        console.log("user :" + socket.id + "  version : " + version);
        if(version != current_version)
        {
            console.log("Version is not equal");
            socket.emit('Version Upgrade', play_store_url);
        }
});

This is Client code

private void Awake()
{
    GameObject temp = GameObject.Find("Socket_io");
    socket = temp.GetComponent<SocketIOComponent>();
    socket.On("Version check", Version_Check);
    socket.On("Version Upgrade", Version_Upgrade);
}

private void Version_Check(SocketIOEvent e)
{
    string version = UnityEditor.PlayerSettings.bundleVersion;
    JSONObject param = JSONObject.CreateStringObject(version);
    socket.Emit("Version Req", param);
}

private void Version_Upgrade(SocketIOEvent e)
{
    Debug.Log("Good");
}

that the infinite client connection start like this.

user connected :  YfzhwDGDuP3U5EtnAAAA
user :YfzhwDGDuP3U5EtnAAAA  version : 0.1
user connected :  W7W3FGsN8UZwWTj6AAAB
user connected :  DHQiaOOHU3qlerbLAAAK
user connected :  xLea9P3RfjNaOWfnAAAL
user connected :  rQncBWatx3YAgM4oAAAM
user :rQncBWatx3YAgM4oAAAM  version : 0.1
user connected :  unciuErN6CJZuLkoAAAN
user :unciuErN6CJZuLkoAAAN  version : 0.1

... 慠慠 i don't know why this thing happens...



via ģ“ķ›„ģ„±

ECONRESET socket hungup

I have a function that triggers on firebase database onWrite. The function body use two google cloud apis (DNS and Storage).

While the function is running and working as expected (mostly), the issue is that the Socket hang up more often than I'd like. (50%~ of times)

My questions are: Is it similar to what the rest of the testers have experienced? Is it a well known issue that is outstanding or expected behavior?

the example code is as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {credentials} = functions.config().auth;
credentials.private_key = credentials.private_key.replace(/\\n/g, '\n');
const config = Object.assign({}, functions.config().firebase, {credentials});
admin.initializeApp(config);
const gcs = require('@google-cloud/storage')({credentials});
const dns = require('@google-cloud/dns')({credentials});
const zoneName = 'applambda';
const zone = dns.zone(zoneName);

exports.createDeleteDNSAndStorage = functions.database.ref('/apps/{uid}/{appid}/name')
.onWrite(event => {
    // Only edit data when it is first created.
    const {uid, appid} = event.params;
    const name = event.data.val();
    const dbRef = admin.database().ref(`/apps/${uid}/${appid}`);

    if (event.data.previous.exists()) {
        console.log(`already exists ${uid}/${appid}`);
        return;
    }
    // Exit when the data is deleted.
    if (!event.data.exists()) {
        console.log(`data is being deleted ${uid}/${appid}`);
        return;
    }

    const url = `${name}.${zoneName}.com`;
    console.log(`data: ${uid}/${appid}/${name}\nsetting up: ${url}`);

    setupDNS({url, dbRef});
    setupStorage({url, dbRef});
    return;
});

function setupDNS({url, dbRef}) {

    // Create an NS record.
    let cnameRecord = zone.record('cname', {
        name: `${url}.`,
        data: 'c.storage.googleapis.com.',
        ttl: 3000
    });

    zone.addRecords(cnameRecord).then(function() {
        console.log(`done setting up zonerecord for ${url}`);
        dbRef.update({dns: url}).then(res => console.log(res)).catch(err => console.log(err));
    }).catch(function(err) {
        console.error(`error setting up zonerecord for ${url}`);
        console.error(err);
    });
}

function setupStorage({url, dbRef}) {
    console.log(`setting up storage bucket for ${url}`);

    gcs.createBucket(url, {
        website: {
            mainPageSuffix: `https://${url}`,
            notFoundPage: `https://${url}/404.html`
        }
    }).then(function(res) {
        let bucket = res[0];
        console.log(`created bucket ${url}, setting it as public`);
        dbRef.update({storage: url}).then(function() {
            console.log(`done setting up bucket for ${url}`);
        }).catch(function(err) {
            console.error(`db update for storage failed ${url}`);
            console.error(err);
        });
        bucket.makePublic().then(function() {
            console.log(`bucket set as public for ${url}`);
        }).catch(function(err) {
            console.error(`setting public for storage failed ${url}`);
            console.error(err);
        });
    }).catch(function(err) {
        console.error(`creating bucket failed ${url}`);
        console.error(err);
    });
}



via Adnan Y

Node app fails to bind to heroku port

I'm having trouble deploying my node/express app through Heroku. It runs fine locally, but when fails on Heroku. Here are the logs I'm getting:

2017-04-02T00:38:44.584532+00:00 heroku[web.1]: Starting process with command `npm start`
2017-04-02T00:38:47.292047+00:00 app[web.1]: 
2017-04-02T00:38:47.292065+00:00 app[web.1]: > roommate-matcher@1.0.0 start /app
2017-04-02T00:38:47.292066+00:00 app[web.1]: > node server.js
2017-04-02T00:38:47.292067+00:00 app[web.1]: 
2017-04-02T00:38:47.898461+00:00 app[web.1]: Example app listening at http://:::3000
2017-04-02T00:38:48.496009+00:00 app[web.1]: Hash: b8f55b865340dbca69b8
2017-04-02T00:38:48.496024+00:00 app[web.1]: Version: webpack 2.3.2
2017-04-02T00:38:48.496025+00:00 app[web.1]: Time: 589ms
2017-04-02T00:38:48.496026+00:00 app[web.1]:     Asset     Size  Chunks             Chunk Names
2017-04-02T00:38:48.496027+00:00 app[web.1]: bundle.js  2.81 kB       0  [emitted]  main
2017-04-02T00:38:48.496028+00:00 app[web.1]:     [0] ./src/main.js 43 bytes {0} [built]
2017-04-02T00:38:48.496029+00:00 app[web.1]:     [1] multi ./main.js 28 bytes {0} [built]
2017-04-02T00:38:48.496082+00:00 app[web.1]: webpack: Compiled successfully.
2017-04-02T00:38:48.496027+00:00 app[web.1]: chunk    {0} bundle.js (main) 71 bytes [entry] [rendered]
2017-04-02T00:39:02.372057+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=roommate-matcher.herokuapp.com request_id=03ea4b67-6c85-424d-946a-28386db2c58a fwd="67.164.74.61" dyno= connect= service= status=503 bytes= protocol=https
2017-04-02T00:39:45.102538+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-04-02T00:39:45.102589+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-04-02T00:39:45.211722+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-02T00:39:45.195923+00:00 heroku[web.1]: Process exited with status 137
2017-04-02T00:39:47.542400+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=roommate-matcher.herokuapp.com request_id=e2940321-a403-4420-9065-1851ce0f71d2 fwd="67.164.74.61" dyno= connect= service= status=503 bytes= protocol=https
2017-04-02T00:39:47.657798+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=roommate-matcher.herokuapp.com request_id=370b9dcd-cb90-4ee0-b9a8-83fbb03d229d fwd="67.164.74.61" dyno= connect= service= status=503 bytes= protocol=https
2017-04-02T00:39:48.143930+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=roommate-matcher.herokuapp.com request_id=c3aa1145-1597-48e9-9029-12a55b347501 fwd="67.164.74.61" dyno= connect= service= status=503 bytes= protocol=https

and my server file reads as:

const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const app = express();

const compiler = webpack(webpackConfig);
app.set('port'.process.env.PORT || 3000)

app.use(express.static(__dirname + '/public'));

app.use(webpackDevMiddleware(compiler, {
  hot: true,
  filename: 'bundle.js',
  publicPath: '/',
  stats: {
    colors: true,
  },
  historyApiFallback: true,
}));

const server = app.listen(app.get('port'), function() {
  console.log('Example app listening at http://localhost:'+port);
});

Any help appreciated!



via joanx737

Difference between the tag and get request

I have a perhaps simple question. What would be the difference between an <a> tag and a normal GET request with any element. I know the <a> tag automatically sends you to the url specified in its href attribute. So I assume that a Get request does something similar in it's success callback (as demonstrated below)

But let's say that I also want to send some information along with a normal get request when a for example <span> element is clicked on so I write:

   $('span').click(() => {
    $.ajax({
        url: '/someurl',
        type: 'GET',
        data: {
            title: someTitle,
            email: someEmail
        },
        success: (data) => {
                window.location = '/someurl';

        }
    });
});

Is there any way to achieve this with an <a> tag (the sending information to the so it's available in req.query.title and req.query.email ? Doing this will run my app.get('/someurl',(req,res)=>{})twice because I am sending a GET request to send the data (title and email) and then I am making another GET request when I write window.location = '/someurl' How can I redo this so that it only sends the GET request ONCE but also allows for the sending and storing information to the req object.



via JohnNoob

Loopback offline sync with cordova sqlite

I am creating a cordova application with loopback offline sync using sqlite as its datasource. Here is my code:

lbclient\datasources.json:

{
 "remote": {
    "connector": "remote"
},
 "sqlite": {
    "connector": "sqlite",
    "location": "default",
    "file_name": "sqlite.sqlite3",
    "debug": false
  }
}

lbclient\lbclient.js:

'use strict';

var loopback = require('loopback');
var boot = require('loopback-boot');
var client = module.exports = loopback();

if (client.connectorRegistry) {
   console.log(client.connectorRegistry);
}
client.connector('sqlite', require('loopback-connector-cordova-sqlite'));

boot(client);

On my browser.bundle.js, I noticed that the try block is executed twice

app.dataSource = function(name, config) {
  try {
    var ds = dataSourcesFromConfig(name, config, this.connectors, this.registry);
    this.dataSources[name] =
    this.dataSources[classify(name)] =
    this.dataSources[camelize(name)] = ds;
    ds.app = this;
    return ds;
  } catch (err) {
    if (err.message) {
    err.message = g.f('Cannot create data source %s: %s',
    JSON.stringify(name), err.message);
  }
    throw err;
  }
};

Here's the dataSourcesFromConfig:

function dataSourcesFromConfig(name, config, connectorRegistry, registry) {
  var connectorPath;

  assert(typeof config === 'object',
  'can not create data source without config object');

  if (typeof config.connector === 'string') {
     name = config.connector;
     if (connectorRegistry[name]) {
        config.connector = connectorRegistry[name];
     } else {
        connectorPath = path.join(__dirname, 'connectors', name + '.js');

        if (fs.existsSync(connectorPath)) {
          config.connector = require(connectorPath);
        }
     }
    if (config.connector && typeof config.connector === 'object' && !config.connector.name)
        config.connector.name = name;
    }

   return registry.createDataSource(config);
 }

On the first run of dataSourcesFromConfig, the db (sqlite) is initialized just fine but on the second run, I now get the error:

browser.bundle.js:93172 Uncaught TypeError: Cannot create data source "sqlite": fs.existsSync is not a function

Why am I encountering that? Can you please help me. Thank you so much.



via Alisa

how I can save several email templates for individual user via UI?

It would be very helpful to know if someone else has implemented a functionality like this,

I want to give the users the ability to create their own email templates, and save them as templates, in each user's personal settings.

My app can get the variables like, , , ,

Here's how it would look like

enter image description here

Question 1: Meanjs only gives the ability to save Hard coded email templates Where do I save more , per user email templates ? Question2 : Is there a way I can save this in the database ? I'm using mongodb.



via JP.

Set Mongoose query condition based on document’s attribute

I currently have a Mongoose Event document which has startTime, endTime, and duration attributes. I am trying to perform a query in a node.js file that grabs all Events where the event's start time is before the current time stamp, and the event's end time is after the current time stamp plus the duration of the event. Therefore, my query depends on being able to perform a calculation that uses an attribute (duration) from the events it is querying. Something along the lines of this:

    const eventQuery = Event.find(query);
    eventQuery.where('startTime').lte(currentTimeStamp);
    eventQuery.where('endTime').gte(currentTimeStamp + event.duration);

However, I'm not sure what I should write instead of event.duration, as the event has not yet been pulled at this point and rather I want to incorporate each candidate event's duration in the DB into the query somehow. Please let me know if this is possible somehow!



via user3802348

use ng-repeat to display mongoose queries

I want to use angular to show the array I get from mongoose.find(). My approach

controller:

requests:function(req,res){
        corporate.find({request:false},function(err,corp){
            if(corp){
                console.log("yes");
                res.render('requests',{corp});
            }
        });

View:

<!DOCTYPE html>
<html lang="eng" ng-app = "">
<head>
    <meta charset = "UTF-8">
    <title>Business Requests</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h1>hello</h1>
<div ng-init = <%="bus=corp"%>>
    <p ng-repeat = "c in bus">
        Name:  <br>
        Email: <br>
        type: <br>
    </p>
</div>
</body>
</html>

it shows just nothing at all but the header hello



via Mahmoud Youssef

Node js scrapping using cheerio

Anyone Suggest me how to scrape all data related to any specific category of electronic items from any e-commerce site using cheerio. Since I am new in node js. Won't be able to figure out how to scrap data through node js.



via Iron Man

NodeJS Express AMP form req.body undefined

NodeJS, AMP-form, TLS/SSL, Nginx all Least Version.

var bodyParser = require('body-parsre');
app.use(bodyParser.urlencoded({true}); // tried false 
app.use(bodyParser.json());

app.post('/post', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    //res.setHeader('Content-Type', 'application/env+json');
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    res.setHeader('Access-Control-Allow-Origin', '*.ampproject.org');
    //res.setHeader('Access-Control-Allow-Origin', '*.amp.cloudflare.com');
    res.setHeader('AMP-Access-Control-Allow-Source-Origin', qs.unescape('htttps://mydomain.com'));
    res.setHeader('Access-Control-Expose-Headers', 'AMP-Access-Control-Allow-Source-Origin');
    console.log('sent the name ' + req.body['name']);
    console.log('sent the name ' + req.body[0]);
    console.log('sent the name ' + req.body.name);
    console.log('sent the name ' + req.body.test); // input name test
    console.log(util.inspect(req.body));
    res.end('Yo');
});

<amp-form>
<input name="name" value="a">

amp-form no doubt https://ampbyexample.com/components/amp-form/

Failed to parse response JSON:: Unexpected token Y in JSON at position 0
log.js:458 Uncaught SyntaxError: Failed to parse response JSON:: Unexpected token Y in JSON at position 0 _reported_

token Y is res.end('Yo'); res.end('JKJK'); is token J

result

sent the name undefined
sent the name undefined
sent the name undefined
sent the name undefined
{}



via ć¾ć‚ćŸć‚ć†

Invalid parameter value for redirect_uri: Missing scheme: /auth/google_auth_code/callback

I am trying to get an access and refresh token from Google from a authorization code for the server-side flow. I followed Google's guide here: https://developers.google.com/identity/sign-in/web/server-side-flow.

I am using using passport and passport-google-authcode.

Here are the routes for the node app:

router.get('/auth/google_auth_code',
    passport.authenticate('google_authcode', {
        scope:
        [
            'https://www.googleapis.com/auth/calendar',
            'profile',
            'https://www.googleapis.com/auth/userinfo.email'
        ]
    }),
    function () {
        res.end();
    })

router.get('/auth/google_auth_code/callback',
    passport.authenticate('google_authcode', {
        failureRedirect: '/error'
    }), 
    function (req, res) {
        // do something with req.user
        res.send('hello');
    }
);

Here is the passport config for this strategy.

passport.use('google_authcode', new GoogleAuthCodeStrategy({
    clientID: 'my client id',
    clientSecret: 'my secret',
    callbackURL: '/auth/google_auth_code/callback',
    // passReqToCallback: true
},
    function (accessToken, refreshToken, rawResponse, profile, done) {
            // unable to get here
    }
));

When an authentication request is made, Google responds with the following error:

{
  "error" : "invalid_request",
  "error_description" : "Invalid parameter value for redirect_uri: Missing scheme: /auth/google_auth_code/callback"
}

Here is my credential setup in the Google console:

enter image description here

At this point I don't know what else to do. I have also tried changing the callback URL in the passport.use to an absolute URL. I am definitely getting back a valid auth code (it looks like: 4/Mb2pcOyhYhziROyFHKH5pnzvUldYbAmMop9SJKbBHXQ). Let me know if there is any more relevant information that I can provide.

Thanks,
Sam



via SamB

Javascript String to Regex without Escaping

I have a question regarding regular expressions in nodejs/javascript.

What I am trying to achieve is to convert a regex (loaded from a database as a string) without any escaping to a RegExp object in js.

var regex       = /Apache\/([0-9\.]+) \(([a-zA-Z ]+)\) (.+)/;
var regexString = '/Apache\/([0-9\.]+) \(([a-zA-Z ]+)\) (.+)/';

var str = 'Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny18';

var match = str.match(regex);
var match2 = str.match(new RegExp(regexString));
console.log(match);
console.log(match2);

That's what I tried so far. But as you can see it won't match if the string gets escaped... My output:

[ 'Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny18',
  '2.2.9',
  'Debian',
  'PHP/5.2.6-1+lenny18',
  index: 0,
  input: 'Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny18' ]
null

Am I missing something simple? If not any suggestions for me? Thanks



via Nightforce

Exceeding max redirects when attempting to scrape a webpage?

I'm attempting to scrape a webpage in Node.js.

var request = require('request').defaults({maxRedirects:3});

var propertiesObject = { k:'81684', t:'2177', e:"all", hon:"F", promod:"F" };

let url = "https://webapp4.asu.edu/catalog/classlist?k=81684&t=2177&e=all&hon=F&promod=F"
// , qs:propertiesObject
request({url:url, qs:propertiesObject}, function(err, response, body) {
  if(err) { console.log(err); return; }
  console.log("Get response: " + response.statusCode);
});

I'm getting max number of redirects for some reason. I can do a get request in postman and I can visit the page just fine. What would I do to cause redirects to happen?



via Ryan Shocker

Neither Node.js PUT or POST routes are presenting the data that is being received

I tried this with a number of different modules and methods. Even by proving the other modules do work as expected in Node by building a separate test project and testing each module individually.

Posting FROM Node's hosted router to a remote API (not TO Node's hosted API)

This problem is not one of SENDING data to an API. It must IMO a problem in the receiving API's not giving up the data it IS receiving for some reason.

I've proven the PUT or POST calls are sending the data by sending the call to http://httpbin/org. That site shows me I'm sending what I expect to be sending.

Here is how I'm sending. I can even see in the receiving API that that API is certainly getting called successfully.

-- sending -- ((Again. This shows my node.http attempt. But I get the same problem using requestjs, requestifyjs, needlejs))

router.get('/', function (req, res, next) {

    var hst = req.headers.host.split(':');
    var lookbackURL = 'https://' + req.headers.host + req.baseUrl;
    lookbackURL = 'http"httpbin.org/put';
    var dat = {
        what: 'ever'
        , try: 'again'
    };
    var bdy = JSON.stringify(dat);
    var options = {
        host: hst[0], port: hst[1], path: req.baseUrl, method: 'PUT'
        , headers: { 'Content-Type': 'application/json' }
    };

    var r = nodeHttp.request(options); r.write(bdy); r.end();
    res.sendStatus(200);

});

-- receiving --

router.put('/', function (req, res, next) {
    console.log('r', req);
});

No matter what module or method I use, in all cases, the receiving req object doesn't contain the what or try data.

BUT in my test project the data is there as I expect it to be, in all cases. Doing the same console.log(req); in the test project, reqestjs, requestjs, needlejs, node.http all show a proper body object. But in this problem there isn't a body object in req. And sending this put/post to http://httpbin.org I can see the body object is being sent.

Any ideas?



via user2367083

Collaborative Paint App

I am in the early stages of learning nodejs client/server programming, for my first application I just want to make a simple collaborative drawing app like paint. So far I have this code and the only thing that seems to work is that my click properly calls in my console.log but no rectangle draws at the click location. Any help? I am pretty new to this.

var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
        
        SOCKET_LIST[socket.id] = socket;
        
        var self = {
                x:0,
                y:0,
                cx:0,
                cy:0,
        }
        
        socket.on('mousePos', function(data){
                self.x = data.x;
                self.y = data.y;
                //console.log('x:' + self.x + ' y:' + self.y);
        });
        
        socket.on('mouseClick', function(data){
                self.cx = data.x;
                self.cy = data.y;
                console.log('x:' + self.cx + ' y:' + self.cy);
        });
        
        socket.emit('paint', {x:self.cx, y:self.cy}); 
        
        /*for (var i in SOCKET_LIST){
                var socket = SOCKET_LIST[i];
                socket.emit('paint', {x:self.cx, y:self.cy}); 
        }*/
        
});
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

<div id="gameCanvasDiv">
        <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
</div>
        
<script>
        var socket = io();
        
        var ctx = document.getElementById("ctx").getContext("2d");
        
        ctx.fillStyle = "#FF0000";
        
        setInterval(function(){
                
                
    },40);
        
        document.onmousedown = function(event){
                
                socket.emit('mouseClick', {x:event.clientX, y:event.clientY});
        
        }
        
        document.onmouseup = function(event){
                
                socket.on('paint', function(data){
                        ctx.fillRect(data.x,data.y,10,10);
                });
                
        }
        
        document.onmousemove = function(event){
        socket.emit('mousePos', {x:event.clientX, y:event.clientY});
    }

</script>


via Jaekx

npm install errors with window's CMD

I'm new to NODEjs trying to globally in using window 8.1 run as ADMINISTRATOR from CMD

install an npm module i.e. node-dev

Every time I try to npm install I get this error.

npm ERR! Windows_NT 6.3.9600

npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\
node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "node-dev"

npm ERR! node v6.10.1

npm ERR! npm  v3.10.10

npm ERR! code ENOTFOUND

npm ERR! errno ENOTFOUND

npm ERR! syscall getaddrinfo

npm ERR! network getaddrinfo ENOTFOUND registry.nmpjs.org registry.nmpjs.org:80

npm ERR! network This is most likely not a problem with npm itself

npm ERR! network and is related to network connectivity.

npm ERR! network In most cases you are behind a proxy or have bad network setti
gs.
npm ERR! network

npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

I did some searching around, and saw a couple of similar cases none of which have been resolved for the case of windows.

So what's the deal? Is this some weird fringe case bug that has no solution yet?



via Ahsan

get data from mqtt broker -6lbr

I followed this tutorial :

Running the CC26xx Contiki Examples

but instead of using the cc26xx-demo I used the cc26xx-web-demo and successfully manged to get everything up and running and I can access the 6lbr web page, when I access the sensorTag page I see a mqtt configuration page as shown: sensorTag page

and if I click index in the sensorTag page (pic above) I get to see the data:

enter image description here

the question is , how can I write a simple nodejs js file that uses the mqtt broker information to grab all the sensorTag sensors data and save it in an local object.



via HummingDev

I can not click on a link with phantom-node

I tell you what I want to do. I am doing a scraping of tematika.com site to be able to create a base of books to me. The problem is that I need to go from page to page but I can not, so I always get the same books. I think with the code they will understand it better.

const phantom = require("phantom");
const cheerio = require("cheerio");

let _instance, _page;
let _books = [];

phantom.create().then(instance => {
 _instance = instance;
 return _instance.createPage();
}).then(page => {
 _page = page;
 return _page.open('http://www.tematika.com/catalogo/libros/arte__arquitectura_y_diseno--9.htm');
}).then(status => {
 console.log(status);
 return _page.property('content')
}).then(html => {

 // I transform the html into an object to manipulate it with cheerio
 let $ = cheerio.load(html);

 //I get all the books and pass them to an array to go through them and extract the info I'm looking for
 let books = $('.Gcentro').find('.moduleproductob').toArray();

 //As always there are 10 pages I make a for it it itere 10 times
 for(let i=0;i<10;i++){
  books.forEach(book => {
  let _book = {};
  _book.imageMin = $(book).find('.Gimagesproductos').attr('src');
  _book.linkBook = $(book).find('.FProductos').attr('href');
  _books.push(_book);
 });

 //In this part what I look for is to click on the next button on the page and it is what is not working for me
 //What ends up happening is that it never goes back to the page. So always bring me the info of the same 10 books.
_page.evaluate(function() {
 var evObj = document.createEvent('Events');
 evObj.initEvent('click', true, false);
 document.getElementById('catalogNext').dispatchEvent(evObj);
});
}
  console.log(_books);
  _page.close();
  _instance.exit();
}).catch(e => console.log(e));

What I need is to be able to click on next and be able to bring me the books that are showing. If it is not with phantom-node and cheerio can be with another tool, all I need is that it works on node.

Thank you! :)



via Exequiel Demaio

How to convert gif in mp4 video with Nodejs+Python?

Is there a way to convert a .gif uploaded image into .mp4 with only Nodejs? I see there are some tutorials on doing it with Python. How do I integrate that with a MEAN app? If Python is the only way to go about it, how do I call the Python script from my Nodejs? I want to be able to store the converted .mp4 file to S3.

Many thanks.



via Somename

Whaleclub api opening a turbo position

Im trying to test opening a turbo position in the whaleclub api

http://docs.whaleclub.co/

and so far I have

wc.newTurboPosition({
direction: 'short',
market: 'BTC-USD',
type: '1min',
size: (1 * 1e8)/100 
}).then(console.log);

using this client library

https://github.com/askmike/whaleclub

I can open a regular position fine using

wc.newPosition({
direction: 'long',
market: 'BTC-USD',
leverage: 10,
size: 1 * 1e8, // 1 btc
}).then(console.log);

nothing shows up in my log either

so not sure why its not working for the turbo position and I tried changing the size too since the turbo market has .1 btc limit but that didnt work either. I am very new at this and thank you in advance



via AK0101

Best stack for new project (think simple salesforce/ glorified todo for job seekers/recruiters) Rails 5, React, Redux, AWS, React Native(native app)?

Trying to decide what the best stack would be for a new project, eventually would like to make it production ready and get funding, currently no team, just me.

Thoughts on stack, concerns, suggestions, recommended tools? Anything would help!

Below is the stack i'm thinking of using and reason:

Rails 5

  • Already know Ruby/Rails
  • Can produce an MVP quickly (no team, just me)
  • Rails 5 has API mode (could reuse backend with different front-end later)
  • Predictable out of the box
  • Less decisions need to be made initially

React/Redux

  • Latest features
  • Learning Redux/React
  • Speed, modulation
  • Rails Gems make it easy to use with Rails
  • Could also use NPM on front-end if integrated with Rails 5
  • Believe it'll still be relevant years from now

React Native

  • Hear its the best option if i'm using React
  • Need native app
  • Don't have team to build IOS & Android

AWS

  • Need to learn/use AWS
  • Have free $1k credit
  • Mature, reliable and secure
  • Would help when project gets ready for production

Alternatives:

  • Angular 2+(already know basics)
  • Ionic
  • Node & Express (need to learn Express)
  • MongoDB(need to learn) I've heard data disappears :(.
  • Firebase for database, storage, messenger, notifications ???


via Ricardo Gomez

Why does invoking toObject() on a mongoose document delete a property when called with getters set to true?

When invoking toObject() on a document with a (nested) property which is set to null, the property will be erased but only when supplying the getters option with true, see example below.

var mongoose = require('mongoose');

const schema = new mongoose.Schema({
   customer: {
      name: { type: String, required: false },
   },
});

var Model = mongoose.model('Model', schema);
var model = new Model();
model.customer = null;
console.log(model.toObject());
console.log(model.toObject({ getter: true }));

//Prints: 
//{ _id: 58e00dcd71bf5916802bdb3c, customer: null }
//{ _id: 58e00dcd71bf5916802bdb3c, id: '58e00dcd71bf5916802bdb3c' }

Why is this?



via Christian

Posting FROM Node's hosted router to a remote API (not TO Node's hosted API)

Wanting a Node router that POST Json to some remote API?

I put a lot of effort into this issue this morning so I wanted to share this by offering some comprehensive examples for your benefit.

In each example the router has a GET method that when called, POSTS back to the same router. I'm also showing, very clearly, how to send AND how to access the received data.

In Node.js, in a router, you might sometime what to post from the router to some remote api.

--- using npm install needle -save --- the file routes/nee.js ---

var express = require('express');
var router = express.Router();
var needle = require('needle');

router.get('/', function (req, resp) {
    var dat = { theGreatest: 'ChuckBerry' };
    var lookbackURL = 'http://' + req.headers.host + req.baseUrl;
    needle.post(lookbackURL, dat, { json: true });
    resp.redirect('/');
});


router.post('/', function (req, resp, next) {
    console.log('body.theGreatest', req.body.theGreatest);
    resp.sendStatus(200);
});


module.exports = router;

--- using npm install request -save --- the file routes/req.js ---

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function (req, resp) {
    var dat = { theGreatest: 'ChuckBerry' };
    var lookbackURL = 'http://' + req.headers.host + req.baseUrl;
    request.post(lookbackURL, { json: dat });
    resp.redirect('/');
});


router.post('/', function (req, resp, next) {
    console.log('body.theGreatest', req.body.theGreatest);
    resp.sendStatus(200);
});


module.exports = router;

--- using Node's very own http.request() -- the file routes/nodehttp.js ---

--- When you only want to POST some Json data make your life simpler by instead doing a PUT of the content-type=application/json -----

var express = require('express');
var router = express.Router();
var http = require('http');

router.get('/', function (req, resp) {

    var hst = req.headers.host.split(':');
    var dat = { theGreatest: 'ChuckBerry' };
    var bdy = JSON.stringify(dat);  // you have to take care of this for yourself
    var options = { host: hst[0], port: hst[1], path: req.baseUrl, method: 'PUT'  //PUT!
        , headers: { 'Content-Type': 'application/json' }
    };

    var r = http.request(options);
    r.write(bdy);
    r.end();
    resp.sendStatus(200);
});


router.put('/', function (req, resp) { // PUT. it's a PUT not a POST
    console.log('body[\'theGreatest\']', req.body['theGreatest']); // But here you DON'T have to parse it for yourself.
                 // ^ I'm happy for that even if I am feeling the loss of symmetry.
                 // ^^ At the same this is why your life is easier in a PUT instead of a POST.
    resp.sendStatus(200);
});


module.exports = router;

Enjoy & I hope these more comprehensive demonstrations help you too.



via user2367083

Sending and proceeding POST request

I have very simple node.js server which has

var express = require('express');
var app = express();

app.post('/savearticles', function (req, res) {
    res.send(req.body);
});

and not much more difficult javascript code

    var xmlHTTP = new XMLHttpRequest();
    xmlHTTP.onreadystatechange = function () {
        if (this.readyState === 4 && this.status === 200) {
            alert(xmlHTTP.responseText);
        }
    }
    xmlHTTP.open('POST', '/savearticles', true);
    xmlHTTP.setRequestHeader('Content-Type', 'application/json');
    xmlHTTP.send('postparameter');

It returns undefined (checked by returning (typeof res.body)). What Am I doing wrong?



via R. Korol

node.js Facebook SDK OAuthException

I want to create a server with node.js to cache data like public page feed from Facebook graph API. I need this, because I wanted to make an Android app which pulls a page feed with app access code, but because the app secret should not be used in such app which can be decompiled, I must have an dedicated application server

I'm trying to use tenorviol's node.js SDK but I also have tried all other SDK's for node.js listed on the website of facebook. But I couldn't run any code without getting request or oAuth errors.

I took the app id and secret (MYAPPID and MYAPPSECRET respectively) from my profile page at developers.facebook.com

Also, is there a better way to do this? I'm thinking is "node.js + facebook Node.js SDK + NoSQL" faster than "Apache + NoSQL + PHP facebook SDK" for caching results?

Example:

var fbsdk = require('facebook-sdk');

var facebook = new fbsdk.Facebook({
  appId  : ''MYAPPID',
  secret : 'MYAPPSECRET'
});

facebook.api('/me', function(data) {
  console.log(data);
});

Then I run with node index.js:

{ result:
   { error:
      { message: 'An active access token must be used to query information about the current user.',
        type: 'OAuthException',
        code: 2500,
        fbtrace_id: 'DPWsuCVegfz' } },
  error: true,
  code: 0,
  message: 'An active access token must be used to query information about the current user.' }



via Classicpal

Natively access JSON POST payload from Node.js server

Consider the following HTTP POST call to a Node.js server:

curl -H "Content-Type: application/json" \
     -X POST \
     -d '{"jsonKey":"jsonValue"}' \
     'http://localhost:8080?abcd=efgh'

I would like to access both the URL parameters and the JSON payload of the POST request.

Accessing the URL params is pretty straightforward by importing url.parse:

var server = http.createServer(function(req, res) {
        // Parse the params - prints "{ abcd: 'efgh' }"
        var URLParams = url.parse(req.url, true).query;
        console.log(URLParams);

        // How do I access the JSON payload as an object?
}

But how do I access the JSON payload, using native Node.js library (without any npm import)?

What have I tried

  • Printed req to console.log, but did not find the POST object
  • Read the documentation of req, which is of type http.IncomingMessage


via Adam Matan

Pool error whenever I try and launch my js file

I have this error whenever I launch my js file with node, first I thought it was maybe my database connection but that seems to be right..

Here is the error,

enter code here
at Parser.write (/home/bot/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/bot/node_modules/mysql/lib/Connection.js:103:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/bot/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at PoolConnection.connect (/home/bot/node_modules/mysql/lib/Connection.js:130:18)
at Pool.getConnection (/home/bot/node_modules/mysql/lib/Pool.js:48:16)
at query (/home/bot/site.js:566:7)
at load (/home/bot/site.js:578:2)
at Object.<anonymous> (/home/bot/site.js:58:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)



via Martin Dev

Determine what shell (cmd, bash, or etc) was used to run node.js script

As the title states, I'd like determine what shell (cmd, bash, or etc) was used to run the node.js script (from within the script).

While detecting the OS with the os module can give some clues, it is not enough is some instances, such as as if I were executing from bash on win32 through a terminal like GitBash or Cygwin.

Is there a property somewhere on the process object that has this information?



via bitstrider

mongoose Getting a nested field value from a queried document

I am new to Node js and Mongoose and I am stuck with this one query.

exports.getIndividual=(matchId,phone)=>
 new Promise((resolve,reject)=>{
 var e="matchPersonal.m"+matchId+".bo1";
 query={};
 query[e]=1;

 var quer=usersc.findOne({phone:"8989898980"}).select(query);
 quer.exec(function (err, matc) { 

 var m=matc.toObject();

 console.log('',m);

When I Run this query I get on my console this thing

        {matchPersonal:{m1:{bo1:3}}}

But I just need 3 as my output and I cant use dot operator to read the object properties directly because

   var b=matchPersonal.m+matchId.bo1

doesnt work as '+' cant be used here so please help



via learn_ing

Node Express Can't set headers after they are sent

I know this has been asked in multiple ways already, but my issue seems to be different from those already posed.

I'm trying to send data from a form into google firebase. I have a node app using express.

Here's my function that is sending the data to firebase:

function validateForm() {

async.series([
    function (callback) {
        var errors = "<strong>The following errors were entered:\n";
        var name = $('#name').val();
        var story = $('#story').val();

        if (name.length < 1) {
            errors += "\n-Please enter a valid name";
            callback("Please enter a valid name", null);
        }

        if (story.length < 1) {
            errors += "\n-Please enter a valid story";
            callback("Please enter a valid story", null);
        }
        console.log("FInished validating");
        callback(null, "finished validating");
    }, function (callback) {
        firebase.database().ref('stories/' + Date.now()).set({
            name: name,
            story: story
        }, function() {
            console.log("Firebase callback");
            callback(null, "sent data!");
        });
    }, function (callback) {
        console.log("Finished!");
        callback(null, "done")
    }
])

}

(added come console.logging for clarity to ensure I had the callbacks right)

The submission is trigger by clicking on a div that's styles to look like a button, so I know there's no default behavior of forms issues that are causing the problem. Below is the error I'm getting in Node.

Error: /Users/keegan/WebstormProjects/hack4health/views/error.hbs: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) at ServerResponse.header (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:719:10) at ServerResponse.send (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:164:12) at res.render.done (/Users/keegan/WebstormProjects/hack4health/node_modules/express/lib/response.js:956:10) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:93:9 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:88:18 at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:69:11 at done (/Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/async.js:74:20) at /Users/keegan/WebstormProjects/hack4health/node_modules/hbs/lib/hbs.js:64:20



via thekeegs

protractor without selenium server

When I issue the protractor command at the command line, with the following configuration:

I get this:

$ protractor
[12:22:23] I/launcher - Running 1 instances of WebDriver
[12:22:23] I/local - Starting selenium standalone server...
[12:22:24] I/local - Selenium standalone server started at http://10.0.0.242:55414/wd/hub
Started
.


1 spec, 0 failures
Finished in 8.223 seconds

[12:22:33] I/local - Shutting down selenium standalone server.
[12:22:33] I/launcher - 0 instance(s) of WebDriver still running
[12:22:33] I/launcher - chrome #01 passed

the problem is that it takes 5+ seconds to start up the "selenium standalone server".

Two questions - (1) do I need this server to run the tests? And (2), is there a way to run the server in the background without having to restart the server everytime?



via Alexander Mills

nodejs - change contents of zip file without re-generating the whole archive

I'm using node-zip (which uses JSZip under the hood). I need to modify the contents of a zip file, and I'd love to be able to modify it without generating the entire zip again, because it can take a long time for large archives. Here's an example:

var zip = new JSZip()

// Add some files to the zip
zip.file('file1', "file 1 contents\n")
zip.file('file2', "file 2 contents\n")
zip.file('file3', "file 3 contents\n")

// Generate the zip file
buffer = zip.generate()

// Make some changes
zip.file('file1', "changed file contents\n")

// Now I have to generate the entire zip again to get the buffer
buffer = zip.generate()

How can I do something like

updatedBuffer = zip.updateFile(buffer, 'file1', 'changed file contents\n')

Where I get an updated archive buffer but I only have to spend CPU cycles updating the one file



via ZECTBynmo

node.js, for-loops, and twitter API

I am trying to only fetch the text of the tweets for a twitter bot I am working on (keep in mind twitter's API docs are worse than Oracles, which I never thought that was possible, anyway). I was using the following link to make my bot https://community.risingstack.com/node-js-twitter-bot-tutorial/

I assume that it is within their randomTweet function where they get the index from.

function ranDom (arr) {  
  var index = Math.floor(Math.random()*arr.length);
  return arr[index];
};

and later in their code they would reference it using

Twitter.post('favorites/create', {id: randomTweet.id_str}

I am trying to simply access the text using a regular foreach loop (seeing as, as far as I am aware, node.js has no concept of an indexed for loop) as follows:

var tweet = data.statuses;



    for(var result in tweet) {
        console.log(result.id_str);
    }  

However, all I am getting is undefined, and I am not certain as to why since for them it worked fine when they passed in a specific index.



via SomeStudent

How to debug firebase web app locally

I need to build a firebase function, so i am trying to set up a developer environment where i can test and debug a firebase function.

From the examples i saw, it all ends up setting up a nodejs node. Because i can't run the node directly, i need to use firebase CLI to start it, so i can't use any local debug.

The best thing i can come with is to put the code in html page and use the browser native debug, which is ok, but database handlers do not work.

Debugging on the server, looks to complicated for short cycles.

Is there a better way to debug it locally?



via ByteArtisan

Stuck in multiline edit mode when importing module in Node.js REPL

I'm on node v7.5.0. I run node and then:

> import moment from 'moment';
... moment();
...

I get stuck in multiline mode as you can see. How do I use import in Node REPL?



via Liron Yahdav

How to combine multiple npm modules?

I like to learn by doing, and I would like to learn to use npm modules for more flexible and rapid coding - not "monolithical.

I have been using a graph visualisation - vivagraph, which can be assembled with npm modules - https://github.com/anvaka/ngraph.

Could you show an example explaining how to compile npm modules into one bundle (as an example, a few ngraph modules into a custom vivagraph bundle) and which tools to use for a "correct" approach in programming and extension for writing new modules?

I have learnt using JS "monolithically" by myself thanks to examples on the graph visualisation library, and I am bit overwelhmed by require, browserify, npm.. and how to combine them to make coding simpler.

My typical need is using JS for front-end only (not as a server).



via user305883

node.js needle post not doing its job

Why doesn't Needle do its job here? It just seems to hang forever waiting to time out. Eventually I will get the

Ready-o, friend-o

var express = require('express');
var router = express.Router();
var needle = require('needle');

router.get('/', function (req, resp) {
    console.log("nee.get");

    var data = { key: 'value' };
    needle.post('http://localhost/nee', data, { json: true })
        .on('done', function () {
            console.log('Ready-o, friend-o.');
            resp.sendStatus(200);
        })
});


router.post('/', function (req, resp, next) {
    console.log(req.body.key);
    res.sendStatus(200);
});


module.exports = router;



via Steve

How to define webpack dynamic context where node.js code try to find a filename with fallback?

I try to webpack a node.js module and stumbling on some code where we try to require a JSON file with a fallback to a file that will exist at all events. The following code is a hard nut for webpack.

var tryRequire = function(filename){
  var result = null;
  if (fs.existsSync(filename)){
    result = require(filename);
  }
  return result;
}

var getI18nStorage = function(culture){
  var cultureParts = culture.toLowerCase().split("-");
  var dir = path.join(__dirname, "../../i18n/address");
  var fn = cultureParts[0] + ".json";
  var filename = path.join(dir, fn);
  var storage = tryRequire(filename);
  if (!storage){
    fn = cultureParts[1] + ".json";
    filename = path.join(dir, fn);
    storage = tryRequire(filename);
    if (!storage){
      fn = "us.json";
      filename = path.join(dir, fn);
      storage = tryRequire(filename);
/*      if (!storage){
        console.log(culture, "nothing found");
        process.exit(1)
      }*/
    }
  }

  return storage;
}

Currently packing var c = getI18nStorage("en-GB"); results in !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; which is understandable :o)

I tried to learn from https://github.com/webpack/webpack/tree/master/examples/require.context but could not transfer the technique to my problem. I think I need to know 2 things:

  • How to manualy add requiered modules to webpack if they cannot be resolved programaticaly?
    • I tried to add context: path.join(__dirname, "../lib/i18n"), to webpack config but this does not include data to bundled output.
  • Is there a way to help webpack to understand this node.js filesystem strategy to try to find a required module?


via Stephan Ahlf

Unable to read URL encode in nodeJS

I am using express framework in nodeJS and trying to send the activation link on email but as i am activating the link the url get encoded and express routes is unable to read and sending to 404 error page.

Url: 

xxx.com/#/user/543fe901b43083207ff0f863c07135b6/xxx.x@gmail.com

On click to URL:

xxx.com/#%2Fuser%2F543fe901b43083207ff0f863c07135b6%2Fxxx.x@gmail.com

Can you Please help me out what I am doing wrong or something I have missed?

Thanks in advance.



via Sikander

access NodeJS process.env in client side

I am solving a challenging ctf problem in which, the flag is in process.env of nodejs server. I can access source code of server ( bcs they public by express.static....) So is there anyway to access process.env of nodejs in client side?



via Nguyen Dong

How to use Firebase Admin SDK with Node.js?

I want to use the Firebase Admin SDK but I have never used Node.js before. I'm using Brackets as my editor and it has a built in web server because I need it for my AngularJS routes. I run my web app through Brackets but I get "require is not defined" in the console.



via Zik

'npm' is not recognized as an internal or external command, operable program or batch file, but it's in my env path

I'm trying to launch on Windows a node.js module that I have written on Linux.

I have set the env path to nodejs, i have installed my dependencies, when i use npm alone it's working. But when I npm start my module, it's telling me this :

my powershell prompt

Here is my package.json's scripts :

enter image description here

Can someone explain to me what's wrong ?

Thanks in advance.



via Devz

Form Submit 404 Not Found Error

Hi I am currently learning NodeJS and the Express framework, but I am having issue with when I try to submit the form it suppose to go the '/users/register' URL but it seems that my app.js is not finding the register.jade file although I have a register.jade under the view folder. Here is some of my codes:

register.jade:

extends layout

block content
    h1 Register
    p Please register using the form below
    ul.errors
        if errors
            each error, i in errors
                li.alert.alert-danger #{error.msg}
    form(method='post', action='/users/register', enctype='multipart/form-data') ...

user.js:

router.post('/users/register', function(req, res, next) { ... });

app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var local = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
...
app.use('/', index);
app.use('/users', users);

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

Error on my Git Bash:

giterror

Error from the browser:

browsererror

Here is my folder structure:

folderstructure

I am not sure why it errors out 404, thank you in advance!



via Cham

Environment variables readable by Apache/PHP and Systemd/node

On the same server, I have a apache php application and a nodejs application (started via systemd).

For systemd/node, I put the following config:

[Service]
EnvironmentFile=/etc/environment

For apache, I have put the following line in /etc/apache2/envvars

. /etc/environment

My problem is:

  • It works in PHP and standalone node if I put export before each variable, but not in node via systemd
  • It works in node via systemd if I remove the export

Is there a way for me to write these variables in a single place that can be used by Apache/PHP and node started via systemd ?



via abd