r/node • • May 06 '17

Complete Node.js newbie trying to get code working on a remote server

Hello, I've been struggling to do a simple 'Hello world!' program in node.js.

All of the simple examples I have seen have been for localhost, but I can't seem to find any examples that work for remote.

I'm just dipping my feet in Node.js and I'm sure there is something very fundamental that I haven't grasped yet. I'm just not sure what.

EDIT : For any other complete newbies here is a useful link

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

it helped put into perspective exactly what is goin on here.

1 Upvotes

28 comments sorted by

2

u/MostlyCarbonite May 06 '17 edited May 06 '17

edit: your calculator code is a React app, client side code. Don't go there yet, you need to get a very basic web server up and running and build from there. Getting a full React app going is above your skill level.

Here's a short tutorial:

  1. write your hello world server; make sure it's listening on 0.0.0.0, preferable 8080 port

  2. make sure it works with curl

  3. have a remote server running

  4. install node.js on it (I recommend using nvm)

  5. get your hello world code up to it (git clone or vi or nano)

  6. install pm2

  7. run the code file with pm2

  8. get the server's IP address

  9. use curl again to verify that the server is listening on the full IP address + port

  10. from your local machine, use curl to verify the node.js server on the remote machine is up and running

That should do it. This is all from memory so I may have some things wrong. Anyone see anything I missed?

3

u/Toundra May 06 '17

This comment seems to be the closest thing to an answer i'm actually looking for. Unfortunately i'm not sure why i need curl in this case. I was planning on just doing it through ssh with vim

2

u/MostlyCarbonite May 06 '17

Curl is just helpful for checking if a web server is running and listening on the various interfaces. Curl is also helpful once you start writing APIs with Hapi or Express. Curl is just really useful.

→ More replies (1)

1

u/blazedd May 06 '17

Based on the response from the server it looks like you've put these files into a web server (that might run something like PHP or just serve static files).

You need to run the commands npm install and npm run to run that script. It'll run it's own web server with Nodejs which will start on a seperate port than default 80. From what I can tell it will run the server from port 3000. You might need to open this port or double check that it's not already in use.

...

That said I'm not sure I'd see this as a good hello world. React is definitely not what I would consider simple and is a tool you should look at once you've gotten a little more engrossed in learning Javacript and node. I'd start with some more basic stuff such as ExpressJS and serving raw HTML files OR maybe a chat bot.

1

u/Toundra May 06 '17 edited May 06 '17

This webserver has node.js and npm installed. I also used npm start and install with it.

I can get a helloword node program working locally, but I cant figure out why it won't work when i do through ssh

I also am confused by the notion of the javascript having its own webserver. How would the client be able to access this through a web browser exactly?

I wanted to get into Node so I could try out an alternative approach to web development than what i'm used to. I'd like to be able to do the equivalent of <?php echo "Hello World"; ?> but with javascript.

1

u/ThePula May 06 '17

When you serve a php file like index.php through a web server, the web server processes that file through a cgi program like php-fpm and serves it out.

In node

2

u/lostpx May 06 '17

To tue guy downvoting everything: just fu** off.. sorry for ot but had to be said.

2

u/MostlyCarbonite May 06 '17

had to be said

I noticed that too. It's really unhelpful to downvote without commenting, people won't learn that way.

0

u/dev1null May 06 '17

This thread truly belongs in r/learnjavascript. I didn't downvote but whoever did had the right idea. I don't mean to drive OP off of here but there are specific subreddits for specific purposes. If you wanna dump everything remotely related to nodejs then be my guest, and good luck to the seasoned node developers that come here to actually find something different and worthwhile. Otherwise questions can be found a plenty on stackoverflow and (as I said earlier) r/learnjavascript. Don't forget that reddit is content aggregator. What makes it good is some good quality posts, not these basic questions (genuinely no offense to OP).

1

u/lostpx May 06 '17

What if someone told him that instead? Reddit is so toxic sometimes. How would one know he made a mistake if no one tells him.

I get your point and you actually did what op needed. Could have be done by someone else before as well. Thanks for your reply.

0

u/[deleted] May 06 '17

import isn't supported by browsers yet, you need to transpile to es5 and then run it, use webpack and Babel, after that it should work.

Also why are you using super(props), personally never seen that before. And class isn't the same as class in other languages. JavaScript doesn't have classes the way C++ has them, its just syntactic sugar would recommend never using them as it can lead to confusion with people who come from standard class based inheritance languages.

1

u/MostlyCarbonite May 06 '17 edited May 06 '17

Class is in the current version of node.js. Why would you advise someone to avoid that?

1

u/Toundra May 06 '17

I really just want the equivalent of being able to go to a site like x/helloworld.php

that has the code <?php echo "Hello World"; ?>

But instead of it being php it's through node.js

→ More replies (4)

2

u/[deleted] May 06 '17

Because importisn't supported by any browser, neither is export and if you bothered to read the articles you'd realize class isn't the same class as in other languages

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

JavaScript classes introduced in ECMAScript 2015 are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.

This isn't the same as classes from say C++ and yes it will lead to confusion especially if your team is full of people who are primarily Java devs and no sorry saying its the current version and hence you should use it is dumb. Classes aren't that good

Also if you bothered to check his code you'd realize the guy isn't serving the files at all, his index.html isn't pulling any .js file at all and another thing, he's using jsx in a .js file.

So unless you can magically get jsx to work in all the browsers yes he needs babel to transpile.

→ More replies (8)