I am tying to get data from vk.com api via https protocol. like this (api vk docs):
var https = require('https');
https.get('https://api.vk.com/method/users.get?access_token=' + global['access_token'], function (d) {
var chunk = '';
d.on('data', function (data) {
chunk += data;
});
d.on('end', function () {
console.log(chunk);
});
}).on('error', function (e) {
console.error(e);
});
but in heroku logs I see
Error: Protocol:https: not supported.
So, how can I get this data? My app work in http protocol, this is config:
var express = require('express'),
routes = require('./routes');
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function () {
app.use(express.errorHandler());
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
Node v0.8.9 added the functionality to make https.get() accept a URL.
Unless you have a very specific reason, use "node": "0.10.x" under engines in package.json.
Also try:
`var options = {
host: 'api.vk.com',
port: 443,
path: '/method/users.get?access_token=' + global['access_token']
};
https.get('options', function(....`
Related
I have an electronic board, which collects data from sensors and I hope to send it to an web service, which then does some processing and sends the results to an website, when URL is entered. I use multiple ports for this. One port listens for UDP connection and other port is for HTTP. The code works fine on my local machine. Here is the code
var net = require('net')
,dgram = require('dgram')
,express = require('express')
,io = require('socket.io')
,routes = require('./routes')
,http = require('http')
,fs = require('fs');
var app = module.exports = express.createServer();
var HOST = '192.168.0.132'
var PORT = 1337
var datarr = []
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout:false, pretty:true});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//ROUTES
app.get('/',routes.index);
//UDP Server
var decoder = new (require('string_decoder').StringDecoder)('utf-8')
var buffer = '';
var server = dgram.createSocket('udp4');
server.on('listening',function() {
console.log('Listening');
});
server.on('message', function(data,rinfo) {
console.log(decoder.write(data));
io.sockets.emit('data',decoder.write(data));
});
server.on('close', function(data) {
console.log('closed');
});
server.bind(1337,'192.168.0.132');
//UDP server ends
var io=require('socket.io').listen(app);
app.listen(1185);
io.sockets.on('connection',function (socket) {
console.log('Hello Got a connection');
});
console.log("server listening");
I know it can't be hosted on heroku, because it allows only one port.
What are my options?
1)According to some answers on this website, websockets. But i have no idea on how to set up websocket between udp and http server. Any links to websites/github would be very helful.
2)Hosting services which allow multiple ports. Are there any which provide this service? Links to documentations will be appreciated.
Thanks in advance.
You can do some thing like this
var port = process.env.PORT || 1185;
and then use this port variable as
app.listen(port);
every time when you need to run on different port just use
PORT = node app.js
what I'm trying to achieve is to accept requests for a value, add that value to an array, run a function on it that'll query it and then remove it from the array with the shift function. What I have so far is a simplified framework of that, but it's now giving me this error.
RangeError: Maximum call stack size exceeded
if there's anything I can do to improve my code as well please let me know.
app.js
var express = require('express')
, http = require('http')
, path = require('path')
, fs = require('fs')
, eventEmitter = require('events').EventEmitter;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
var ee = new eventEmitter;
var queries = new Array();
ee.on('next', next_search);
function next_search() {
console.log(queries);
search();
}
function search() {
// do something
queries.shift();
console.log(queries);
ee.emit('next')
}
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.post('/search', function(req, res) {
letter = req.param('letter');
console.log(letter);
queries.push(letter);
next_search();
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
jQuery code
$(document).ready(function() {
$('#letter').on("keyup", function() {
letter = $('#letter').val();
$.post("/search", { letter: letter}, function(data) {
$('#status').html(data);
});
});
});
This is an infinite function call, when you post. next_search -> search ->ee next event-> next_search -> ...
ee.on('next', next_search);
function next_search() {
console.log(queries);
search();
}
function search() {
// do something
queries.shift();
console.log(queries);
ee.emit('next') // execute first line
// only do this when queries is non-empty
}
You should check if queries is not empty, only then do next_search.
A razor view engine for nodejs.
https://github.com/nufyoot/kally-razor
I have tried something like this.. but no luck.
var razor = kallyrazor({
root: __dirname + '/views/',
layout: 'shared/layout.html'
});
app.engine('html', function (path, options, fn) {
fn(null, razor.render(path, options));
});
Worked for me. Did you include the view engine?
app.set('view engine', 'cshtml');
Note: I chose the use the cshtml extentionm just for vanity.. (And also in Sublime if that is your editor the razor syntax highlighter, https://github.com/joseph-turner/Razor)
Full code:
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var KallyRazor = require('kally-razor');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'cshtml'); // NOTE THE VIEW ENGINE
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Setup KallyRazor
var razor = KallyRazor({
root: __dirname + '/views/',
layout: '/shared/layout.cshtml'
});
app.engine('cshtml', function (path, options, fn) {
fn(null, razor.render(path, options));
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Backbone talking to node/express running locally os x 10.6.8. Trying to populate a clientside backbone model with fetch. Thinking I have fetch wrong. Most of app_stuff.js is cut-and-paste. Curious why node/express sees the request as OPTIONS instead of GET (or POST?) and if I have the terminology right. I think I can make it work like this but would rather pay the dues for some best practices while still an absolute newb.
backbone
(function($) {
var Stuff = Backbone.Model.extend({
});
var Collec = Backbone.Collection.extend({
model: Stuff,
url: 'http://localhost:3000/stuff'
});
var nstuff = new Collec;
nstuff.fetch(
{ data: $.param({ name: "helloworld"}) }
);
})(jQuery);
app_stuff.js
var express = require('express')
, routes = require('./routes')
, stuff = require('./routes/stuff')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/stuff', stuff.index);
app.options('/stuff', function() { console.log("stuffed"); });
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
terminal output
$ node timeline/app_stuff.js
Express server listening on port 3000
stuffed
I'm having a strange issue, possabily a really noob one, but for the life of me I can't get node module.exports to work for me.
Here is my route (routes/about.js)
var About = (function () {
function About() {
this.init();
}
About.prototype.init = function () {
console.log('init');
};
About.prototype.me = function (req, res) {
this.init();
res.json([
'jamie was here'
]);
};
return About;
})();
module.exports = About;
and my app.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, about = require('./routes/about')
, http = require('http')
, path = require('path');
var app = express();
about = new about;
console.log(about);
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/me/?', about.me);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
When about.me is called, i get a "TypeError: Object # has no method 'init'" error...
Any help would be greatly appreciated!
I've just worked it out! Stupid mistake.
I'm passing a reference to the me function, not the object itself. No wonder why it doesn't exist!