How to run server.js on client-side? - node.js

Can anyone explain me how I can run the server.js on client-side. Like I have index.html page and I've a button in it and I want to generate PDF on button click event and pass dynamic data to PDF. I've literally no idea about node.js. Any kind of help is appreciated. Sorry if the question is duplicate.

The simple answer is you don't. Node.js is a back end and the server.js file you have should not run on the browser. If you need the functionality on server.js after the first load you need to use something like ajax to communicate with the server. Or you could try to get your server code to run in the browser, but that may not work for all browsers and is probably a bad practice.

Related

Node Express: How to request data and not a page

I am learning Node.js. This is a very newbie question.
I want to fetch some data from my database via node.js server. I am using Axios and so my line of code looks like this. But it is just an ordinary fetch request.
const response = api.get("/");
But I do not retrieve the data with this. I retrieve a completely new page that looks like this.
It is an empty page with the data that I want to retrieve. But I do not need a new page. I need the data only. I want to store it in the RESPONSE variable. How can I do that?
Can anyone point me in the right direction? Perhaps I should watch a video on how to build a REST API?
This is not a page. It is your browser's way of rendering the data of your response. You can use a JSON viewer plugin for your browser if you expect to see pretier data or use an HTTP client like Postman to be sure that your API returns what you expect.
So this was the problem.
My React was on port 3000. My Node was also on port 3000. When I started my React App, everything was fine and my browser displayed all pages perfectly well. But once I started my Node, it rendered data into the browser.
Just now I changed the Node port to 4000 and the problem is gone.
It seems like React and Node were competing here who is going to render the content into my browser. Node was prefered by my chrome browsers. Perhaps because I started it later.

Spectron: how to test Electron `shell.openExternal('some url')`

I'm writing and E2E test for an application written in Electron. I need to test a button which calls in effect shell.openExternal('link') where link is an external website. I've been looking in the webdriver docs for something which allows the test intercept this call, but it doesn't look like anything like that exists in the API at all.
1) if something like this does exist an I missed it in the docs, please enlighten me in ways of the light side of the force,
2) if not, then does anyone out there in stackoverflow land have a fancy work-around?
Thanks so much!
I came up with an answer. Instead of trying to intercept the click, I added an env var in the main app such that when set, the click will put an entry into the log instead of actually opening the external link in a browser. Then I use the API in spectron to slurp up the render process logs: https://github.com/electron-userland/spectron#clientgetrenderprocesslogs
Then I can just look for a custom string in the logs and I can judge the proper text is present.

Can I merge a node server with the polymer init starter-kit application?

I have my fledgling polymer application running. I would like to load and save a file. I would like to do that by using the ajax element. But what happens on the server side?
I have made nodejs express applications before. I could make a separate server for the client to talk to. But there are at least two other options:
Take the front-end material supplied by the polymer start-kit and put it in the /public directory of an express application.
Put express routes in my polymer starter-kit application.
I am inclined to bring Express into the existing start-kit application. But maybe someone else has already tried this, and can tell me what I will run into?
I looked at the starter-kit code a little bit. Apparently, it uses the spdy package and not the http package, but I can work with that. That's as far as I have gotten. Any advice?
Regards, Rick
I talked to the guys on the Polymer slack channel. Thanks, milesje. polymer serve is just supposed to be a light server to serve static files. If you want to do something more substantial, you should create your own server (nodejs or something else) and put your Polymer files in the public/ directory.
Hope this saves someone else some time.

Responsive design for working website

I'm beginner web-developer, front-end only. Sometimes i need to make existing working websites to be responsive. I use browser extension(Styler for Chrome), that show me window where i can insert my styles, which will be applied for a page. But it looks little difficult(need to write code in my text-editor, copy this to extension form, than again, again and again...). Is there a way, to integrate my local stylesheet, to existing website, make changes only in my editor and reload page automatically, like with local page? I've found something on LiveReload website -
http://feedback.livereload.com/knowledgebase/articles/86220-preview-css-changes-against-a-live-site-then-uplo, but I can't use their app, cause i'm on windows(LiveReload is still in beta on it). If anybody use similar, can you please explain how to get it to work? Thanks.
In process of expiriencing I found simple solution for this:
Need to install Chrome extension(CSS Inject).
Run web-server on your machine which will host your css file for injecting(CSS Inject works only with HTTP) and insert it to CSS Inject, in my situation it looks like - http:// adapt/css/style.css
Need livereload server. I'am using node.js and this package https://www.npmjs.com/package/livereload for this.
Create file in your web-site root(for example server.js)
Paste this code in server.js:
var livereload = require('livereload'),
server = livereload.createServer();
server.watch(__dirname + "/css");
console.log('waiting for changes');
Go to your live website and activate CSS Inject.
run node ./server.js
That's it. You can now modify your styles localy and see changes on real website.
If anybody knows better solution(using API from this package https://www.npmjs.com/package/livereload#api-options, specifically overrideURL option) or have better expirience with node.js and understang how to implemet it, please post your solution here, I will be grateful.

How to show popup message using Node.JS

I wanna show some popup message from server side javascript(Node.JS) code.
exports.ShowPopup = function(req, res){
window.alert("Pradeep");
};
This was i tried. But while redirecting to this path i'm getting the error like
" window is not defined "
So can u please help me on this.Thanks in Advance.
With regards
Pradeep Raj. K
This is not possible. You cannot control the users browser with server side executed code.
I think you misunderstood NodeJS. NodeJS is a binary that runs javascript as server side code. It works just as any other scripting language on a server like Python or such.
You tried to use the window object, which is exclusive to the (or most) browsers API and does not exist in the regular Node environment. Also you don’t have any GUI, so you won't be able to accomplish what you are trying. Just use regular front end javascript for this.
PS: I assume you meant showing a popup in the browser, right?

Resources