open webpage when start nodejs server [duplicate] - node.js

This question already has answers here:
How to use nodejs to open default browser and navigate to a specific URL
(8 answers)
Closed last month.
I was wondering if and how i can automatically open a webpage when i start my server with nodejs
app.listen(app.get("PORT"), () => {
console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
})
I have this basic example where when i run node start proyect it will start the server and then i manually need to click on the console logged message or open a browser and type bla bla bla
I want to raise that web for itself like when in React we run the command npm run start
I tried to google it but i dont know how to search for this because i dont get any usefull info.
Thank u very much

Use open to Open stuff like URLs, files, executables. Cross-platform.
const open = require('open');
// opens the url in the default browser
open('http://stackoverflow.com');
UPDATE
open simplifies cross platform functionality, in node you can have access to the platform by process.platform and write platform specific executions
for example here is the native version in mac without using any NPM module
const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);
I don't have access to a windows system to write the code for windows

Related

How to get Node.js http request to work from a browser on Siteground (shared hosting)?

Ok, I've searched around a bunch, and maybe the answer is here and I just couldn't find it because I wasn't using the right search combination or whatever, so please bear with me!
I'm very new to Node and all the tutorials I've read/watched (understandably) use localhost with a port number for examples. I'm having difficulty translating that into a real, shared-server website.
I successfully got Node.js installed on my shared hosting on Siteground (I can check versions, run javascript by pulling up node through putty, etc.).
I can start an http server using port 3000 and my ip address, and I can confirm that the server listener is working using another putty window and curl ipaddressORdomainname: 3000 (for testing purposes until I get it connected). I'm running the following to start the server:
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url, req.method);
});
server.listen(3000, 'my_ip_address', () => {
console.log('listening for requests on port 3000')
})
And running curl ipaddressORdomainname: 3000 in another putty window successfully displays / GET in the other putty window with the open port.
What I can't seem to do is use any web browser (tried latest versions of Brave, Chrome, Edge, Firefox, Opera to no avail) to "connect" and show the same result I get from the curl line.
I'm ultimately trying to connect a website to the node.js server, and I think this is my final missing piece I can't seem to find a solution to.
Any guidance would be very appreciated.
Thanks!

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

How to get nodejs on client-side on cordova?

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.
You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

Generate node-xmpp script for browser

I am developing an application on php. I need a chat on xmpp nodejs. Sending a message will go from web page.
I found enter link description here. In terminal everything works fine. But how do I attach client script to the browser?
I generate script by:browserify node_modules/node-xmpp/lib/node-xmpp-browserify.js > nodeXmpp.js and attach it to web page:
Then trying to use it:
$(document).ready(funcrion(){
var client = new XMPP.Client({
jid: 'user#example.com',
password: 'password'
});
});
And chrome console telling me:
Cannot load StringPrep-0.2.3 bindings (using fallback). You may need to npm install node-stringprep nodeXmpp.js:3669
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery.js:3254
Uncaught TypeError: Object # has no method 'resolveSrv'
Object # - its "dns".
And before generate script i install node-stringprep.
Question is how build xmpp client script for browser.
You didn't post your code, but the following should work fine:
require('node-xmpp/lib/node-xmpp-browserify.js');
var client = new XMPP.Client(opts);

now.js how to get 'now' object without browser (simple client)?

Im playing around with node.js and now.js. Everything works fine. But i would like to make a simple client that i can run from the command-line (so without the browser).
http://nowjs.com/doc/example
In the example a HTML page gets served, and that page includes the now.js file, which creates the magic 'now' object. But on a commmand-line there is no such thing.
For the server i have running (helloworld_server.js)
And the client helloworld_client.js i have:
// client.js
var nowjs = require("now");
// now i need to connect to the server (127.0.0.1:8080)
// so i i need a server object?
server = ????
var everyone = nowjs.initialize(server);
everyone.now.distributeMessage('hi!');
So how do i obtain the 'now' object?
OK, got it. Once you installed now
npm install now
it creates a node_modules folder, inside you see folders for each extension. Deeper you find:
./node_modules/now/examples
and there is the nodeclient_example folder
./node_modules/now/examples/nodeclient_example
its pretty clear from there, but those curious, this is the magic you need:
var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');
and there it it the 'magic' now object
make sure you install :
npm install socket.io-client
otherwise it wasn't working for me!

Resources