Running node-js code inside angular2 app - javascript

How can i run this code from angular2 app component?
var request = require("request");
var fs = require("fs");
var img = "http://vignette1.wikia.nocookie.net/custombionicle/images/a/ae/Kitten_with_gun_final.png";
request(img).pipe(fs.createWriteStream('image.png'));

Unfortunately, node.js is the backend part of the website and angular the frontend, you can't mix them together. What you can do instead is to run the pice of code on the backend and if there's some data you need it on the client side, to send it to the client ;)

Related

Primus call to nodejs server, primus file not found

I've set up an API with nodejs express for a real-time chat application. For it to be real-time I am using primus but I'm currently stuck at trying to connect primus to my frontend.
I have a folder structure for the whole backend and then another folder structure for my frontend. So they are both separate.
Here I connect the server to Primus
var server = http.createServer(app);
const primus = require('../primus/live').go(server);
This then goes as you can see to the folder primus with a file live.js
//BACKEND
const Primus = require('primus');
let go = (server) => {
let primus = new Primus(server, {/* options */});
primus.on('connection', (spark) => {
console.log('Received spark 🔥');
});
}
module.exports.go = go;
Now in my frontend, I am trying to call Primus via the script tag
//FRONTEND
<script src="http://localhost:3000/primus/live.js"></script>
but this just gives me a 404 Not Found error. Also when I just try to connect through this in my browser it doesn't work. So I am unsure what my problem here is. Any ideas?
https://github.com/primus/primus#how-do-i-use-primus-with-express
make sure to call .listen on the http server, not the Express server

NodeJS Server Declaration

I'm fairly new to nodeJS/Express which I'm learning at the moment.
There seems to be different methods of creating a http server and I'm wondering what the difference is. E.g.....
From a socket.io tutorial:
var app = require('express')();
var http = require('http').Server(app);
...and from a nodejs tutorial:
var express = require('express'),
app = express.createServer();
Can someone explain the difference between the two, particularly in regards to the first example? I'm presuming the empty brackets after the express require is an anonymous function, but what is that performing? Why pass the app to the Server method?

How to build socket io server in different file except app.js

I am pretty new to node.js and now I am doing a project on building a website on node.js. Sorry if my question is very naive.
I am using express framework.
My app.js is listening at port (3000).
In my route.js, I got some data from calling some API. I want to display the data to my datapoint.jade file. And because I want to draw those datapoint I have to embed a javascript file in jade. I want to use socket.io to achieve this sending and receiving data.
However, all the examples to construct socket is in app.js. I get data in router.js(in one of its callback function). app.js require router.js.
How can I send data from route.js to app.js and then send out to client side.
Could you please guide me some related and useful information? Or my design would not work at all?
A common paradigm is to pass any dependencies to your child modules:
// app.js
var socket = require('socket.io');
var app = require('express')();
var routes = require('./routes.js')(app, socket);
app.listen();
// routes.js
module.exports = function(app, socket){
var routes = {};
app.use('/', routes.handleIndex)
socket.on('connection', function(){
...
})
}

Socket.io in Express routes file

I'm working on a project which consists in creating a game of the goose like. In order to do that, I'm using Node.js, Express, jade and now Socket.io. But I encounter some trouble, like, in example, to share the position of one client to the other client. Because my variable position is in a function in index.js and I don't know how I can use Socket.io in a route file. I try some things, but nothing works.
On internet, I've seen some people who say that there is no-sense to use Socket.io in an express route file. So how can I do that ?
In my index.js I've that :
exports.deplacement = function(io)
{
return function(req,res)
{
//[...]
io.sockets.on('connection', function(socket)
{
socket.broadcast.emit('position', space);
});
res.render('moteur' //[...]);
}
}
And in my moteur.jade I've done this :
script(src="/socket.io/socket.io.js")
script.
var socket = io.connect('http://localhost:3000');
socket.on('position ', function(space) {
alert(space);
})
First of all, I'm not sure what your question exactly means, but if it is what I think it is then I think what you mean by using socket.io in a route file is to be able to include the client side javascript lib provided with socket.io module of Node.
In order to do that, you have to allow the socket.io module to listen to server. This works like a middle-ware itself. Everything has to go through socket.io first before they are routed to the server. So, when you request the client side lib, it is uploaded to the client.
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server)

Supertest custom express server in node

Please be gentle with me. I'm new to async coding and have been thrown headfirst into an intensive project using node to develop and API server. I'm loving it but some things aren't coming naturally.
Our project is built using express js. We have a file, server.js where we instantiate an express server which in turn instantiates our router and so on. I need to integration test this now (partially) complete server. Normally what I do is from the command line run '%node server.js' and then using either python requests or curl make requests and check the responses.
Now I've been tasked with writing a unit and integration test suite so that we can automate our testing going forward. I've been using mocha and now am trying to use supertest for the integration testing. The problem is that supertest expects a server object which it then applies tests to however our file that builds our server object doesn't return anything. I don't want to modify that file so I am stumped as to how to access the server object to use for testing.
My server file looks (in part) like this:
var express = require('express')
var app = express();
// Express Configuration
app.use(express.favicon()); //handles favicon request, which keeps it out of the log when using a browser :)
app.use(express.bodyParser()); //slurps up the body in chunks the node.js way :)
// ...and so on
and my mocha test file looks like this
var request = require('supertest')
, app = require('../server.js')
, assert = require("assert");
describe('POST /', function(){
it('should fail bad img_uri', function(done){
request(app)
.post('/')
.send({
'img_uri' : 'foobar'
})
.expect(500)
.end(function(err, res){
console.dir(err)
console.dir(res)
done();
})
})
})
when I run this test I get a complaint about the app object not having a method named address. My question is, is there a way I can require/call the server.js file so that the app object will be in scope? Or am I going about this wrong. I also played around a little bit with using http.js to make calls directly to the server but didn't have luck that way either. Thanks!
You need to export the app object in server.js:
var app = express();
module.exports = app;
...

Categories

Resources