Sunday, 12 March 2017

elasticsearch - count items after post

I'm using elasticsearch client for node js (working with elastic 2.x), and I want to avoid the exception that is thrown when the maximum number of documents allowed per index is exceeded. I could not find the name of the exception anywhere, so if possible, I would like to get the number of documents in the index at the same query where I POST data to db. Otherwise I will need to run a separate count query before or after each POST. Any type of solution will be appreciated.



via Mister_L

Client side can not take the json data

I have a nodejs server and the code that I have as below. I send an AJAX request and I want the server to send me a response of a json data. When I put res.write()("string data like hello hello") in the server code, client side can take the value of inside and I can see the value on the console. But I cannot get json value with this function. I tried res.end() and res.send() functions as well but it didn't work. How can I send the json value and the following client side code can take the value correctly?

Server side,

app.use('/', function(req, res) {
console.log("req body app use", req.body);
var str= req.path;

if(str.localeCompare(controlPathDatabaseLoad) == 0)
{
    console.log("controlPathDatabaseLoad");
    mongoDbHandleLoad(req, res);
    res.writeHead('Content-Type', 'application/json');
    res.write("Everything all right with database loading"); //I can get this message
    //res.end(json) I can not get json message with this function as well
    res.send("OK");
    //res.send(JSON.stringify(responseBody)); I can not get json message
}

Client side,

function loadDatabaseData()
    {
        console.log("loadDatabaseData");
        var oReq = new XMLHttpRequest();
        oReq.open("GET", "http://192.168.80.143:2800/load", true);
        oReq.setRequestHeader("Content-type", "application/json;charset=UTF-8");
        oReq.onreadystatechange = function() {//Call a function when the state changes.
            if(oReq.readyState == 4 && oReq.status == 200) {
                console.log("http response", oReq.response);
                console.log("http responseText", oReq.responseText);
            }
        }
        oReq.send();

    }



via zoint

Deserialisation is not working properly for JSONAPI-Serialize Module in NodeJS

I am trying to perform serialization and de-serialization using JSONAPI-Serializer module of NodeJS.

Scenario that i wanted to make is user enter the values and value got serialized and then deserialize and show de-serialized output in response.

For this, I have coded a webserver using express module in NodeJS as shown below:-

var express = require('express');
var app = express();
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;


var port = process.env.PORT || 1337;

var bodyParser = require('body-parser');
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/', function(req, res) {

    
    var data = [{"id":new Buffer(req.body.id, 'base64').toString(), "token":"test", "geo":"Test"}];

    var UserSerializer = new JSONAPISerializer('id', {attributes: ['id']});
    var users = UserSerializer.serialize(data);

    var UserDeserialize = new JSONAPIDeserializer();  
    var test = UserDeserialize.deserialize(users).then(a => console.log(a))
    res.json(test);
});
    
app.listen(port);
console.log('Server started! At http://localhost:' + port);

So, I am supplying base64 encoded user input and below are the request and responses for the same.

Request:

POST / HTTP/1.1
Host: localhost:1337
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1
X-Forwarded-For: 127.0.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

id=dGVzdA==

Response:-

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 40
ETag: W/"28-1v0wyAa9HM+8rbZDXQJnnGFWYjo"
Date: Sun, 12 Mar 2017 08:22:11 GMT
Connection: close

{"isFulfilled":false,"isRejected":false}

But I am expecting the response like this:-

[ { id: 'test' } ]

what I am doing wrong here ?



via Ashu

Podcast API: Audiosear.ch audiosearch-client-node

I was looking for a podcast API and came across Audiosear.ch 's SDK client for node.js. I signed up for an applicaiton id and secret from Oauth but have no idea where I'm supposed to enter it in order to use the node. I'm also not sure how to install/setup the node. Any help would be greatly appreciated.



via Fariba

Error: `output.path` needs to be an absolute path or `/`

I am a beginner in React,and at the moment I am trying to do the basic set up. So far I installed the basic modules from npm and the folder structure of my project is as shown below.

folder structure

The package.json shows what I have installed as well as a small description of the project.

{
 "name": "test",
 "version": "1.0.0",
 "description": "test",
 "main": "index.js",
 "scripts": {
   "start": "webpack-dev-server --hot"
},
"repository": {
"type": "git",
"url": "https://github.com/theo82"
},
"keywords": [
"test"
],
"author": "Theo Tziomakas",
"license": "ISC",
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
}
}

Anyway when I am typing npm start,I get this error.

Error: `output.path` needs to be an absolute path or `/`.
at Object.setFs (C:\Users\Theodosios\Desktop\reactApp\node_modules\webpack-d
ev-middleware\lib\Shared.js:88:11)

I believe this has to do with the webpack.config.js file.

 var config = {
 entry: './main.js',

 output: {
  path:'./',
  filename: 'index.js',
 },

 devServer: {
  inline: true,
  port: 3000
 },

 module: {
  loaders: [
     {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',

        query: {
           presets: ['es2015', 'react']
        }
      }
     ]
   }
  }

  module.exports = config;

How can this error be fixed?

Thanks,

Theo?



via Theo

Module not found: Error: Cannot resolve 'file' or 'directory' when running 'npm install' [on hold]

This is the error I get whenever i run 'npm install'

ERROR in ./js/backend.js

Module not found: Error: Cannot resolve 'file' or 'directory' ../../config/env.generated.json in C:\Users\Documents\GitHub\science.mozilla.org/app\js

@ ./js/backend.js 7:20-62



via frankenstein

Saturday, 11 March 2017

Angular 2 project setup with Asp.Net MVC on Visual Studio 2015

I am currently learning Angular 2 and am trying to setup an Angular 2 project with Asp.Net MVC setup on Visual Studio 2015. I referred to couple of articles on Internet and they seems to be too overwhelming complex for a beginner to understand and follow.

I am looking for a simple project setup and I tried to create one but I am getting few errors which I can't seem to solve.

Can anyone help me with resolving the error or redirect me to some articles with simple project setup illustrations?

I have performed the following basic steps,

  1. Created new 'Asp.Net Web Application (.NET Framework)' project with Blank MVC template (no authentication and no unit tests)
  2. Downloaded the 'quickstart-master' angular 2 seed project from Angular 2 official site
  3. Copied the full 'src' folder from seed project to my angular project.
  4. Copied the other remaining files (package.json, bs-config.json, tslint.json, .editorconfig). Basically files which are not mentioned in non-essential-files.txt file
  5. Restored all packages successfully from package.json file
  6. Added a controller 'HomeController'
  7. Added a _Layout.cshtml
  8. Added a _ViewStart.cshtml
  9. Added an Index.html corresponding to Index() action method in HomeController
  10. Copied the JS script references from index.html in seed project to _Layout.html
  11. Copied the body content from index.html to Index.cshtml
  12. Rebuild the solution
  13. Run the solution.

When I try run the solution, I get the below error,

enter image description here

My Project setup looks as below,

enter image description here

Tsconfig.json remains unchanged.

enter image description here

The error,

GET http://localhost:63777/styles.css 
localhost/:14 GET http://localhost:63777/systemjs.config.js 404 (Not Found)
zone.js:2019 GET http://localhost:63777/main.js 404 (Not Found)
scheduleTask @ zone.js:2019
ZoneDelegate.scheduleTask @ zone.js:353
Zone.scheduleTask @ zone.js:196
Zone.scheduleMacroTask @ zone.js:210
(anonymous) @ zone.js:2043
send @ VM96:3
fetchTextFromURL @ system.src.js:1051
(anonymous) @ system.src.js:1778
ZoneAwarePromise @ zone.js:729
(anonymous) @ system.src.js:1777
(anonymous) @ system.src.js:2806
(anonymous) @ system.src.js:3384
(anonymous) @ system.src.js:3707
(anonymous) @ system.src.js:4099
(anonymous) @ system.src.js:4562
(anonymous) @ system.src.js:4831
(anonymous) @ system.src.js:407
ZoneDelegate.invoke @ zone.js:334
Zone.run @ zone.js:126
(anonymous) @ zone.js:713
ZoneDelegate.invokeTask @ zone.js:367
Zone.runTask @ zone.js:166
drainMicroTaskQueue @ zone.js:546
(index):16 ZoneAwareError {__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:63777/main.js
    Error: XHR error…, __zone_symbol__stack: "(SystemJS) XHR error (404 Not Found) loading http:…t>]↵   Error loading http://localhost:63777/main.js", originalErr: ZoneAwareError, __zone_symbol__currentTask: ZoneTask}__zone_symbol__currentTask: ZoneTask__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:63777/main.js
    Error: XHR error (404 Not Found) loading http://localhost:63777/main.js
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:63777/node_modules/zone.js/dist/zone.js:1242:29) [<root>]
        at Zone.runTask (http://localhost:63777/node_modules/zone.js/dist/zone.js:166:47) [<root> => <root>]
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:63777/node_modules/zone.js/dist/zone.js:420:38) [<root>]
    Error loading http://localhost:63777/main.js
        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:63777/node_modules/zone.js/dist/zone.js:1242:29) [<root>]
        at Zone.runTask (http://localhost:63777/node_modules/zone.js/dist/zone.js:166:47) [<root> => <root>]
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:63777/node_modules/zone.js/dist/zone.js:420:38) [<root>]
    Error loading http://localhost:63777/main.js
    at addToError (http://localhost:63777/node_modules/systemjs/dist/system.src.js:122:78) [<root>]
    at linkSetFailed (http://localhost:63777/node_modules/systemjs/dist/system.src.js:687:15) [<root>]
    at http://localhost:63777/node_modules/systemjs/dist/system.src.js:495:9 [<root>]
    at Zone.run (http://localhost:63777/node_modules/zone.js/dist/zone.js:126:43) [<root> => <root>]
    at http://localhost:63777/node_modules/zone.js/dist/zone.js:713:57 [<root>]
    at Zone.runTask (http://localhost:63777/node_modules/zone.js/dist/zone.js:166:47) [<root> => <root>]
    at drainMicroTaskQueue (http://localhost:63777/node_modules/zone.js/dist/zone.js:546:35) [<root>]
    at XMLHttpRequest.ZoneTask.invoke (http://localhost:63777/node_modules/zone.js/dist/zone.js:424:25) [<root>]__zone_symbol__stack: "(SystemJS) XHR error (404 Not Found) loading http://localhost:63777/main.js↵ Error: XHR error (404 Not Found) loading http://localhost:63777/main.js↵        at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:63777/node_modules/zone.js/dist/zone.js:1242:29) [<root>]↵       at Zone.runTask (http://localhost:63777/node_modules/zone.js/dist/zone.js:166:47) [<root> => <root>]↵       at XMLHttpRequest.ZoneTask.invoke (http://localhost:63777/node_modules/zone.js/dist/zone.js:420:38) [<root>]↵   Error loading http://localhost:63777/main.js"message: (...)name: (...)originalErr: ZoneAwareErrororiginalStack: (...)stack: (...)toSource: ()toString: ()zoneAwareStack: (...)get message: ()set message: (value)get name: ()set name: (value)get originalStack: ()set originalStack: (value)get stack: ()set stack: (value)get zoneAwareStack: ()set zoneAwareStack: (value)__proto__: Object
(anonymous) @ (index):16
ZoneDelegate.invoke @ zone.js:334
Zone.run @ zone.js:126
(anonymous) @ zone.js:713
ZoneDelegate.invokeTask @ zone.js:367
Zone.runTask @ zone.js:166
drainMicroTaskQueue @ zone.js:546
ZoneTask.invoke @ zone.js:424



via SamirG