I am still very new to web development so sorry if the answer is obvious!
Anyway, I have created a form for my website that lets users email me a request. I’m using nodemailer
to do this in my app.js
script. Also using Express
, obviously. Anywho, during testing when I run the local server 'node app.js'
everything works fine, ie. I open up my index.html
on localhost:3000
and can successfully send myself an email through my form. Now I just have to add all the files to my website’s cpanel public_html folder. AKA make it official.
THE QUESTION: Is app.listen(*port number*)
necessary at all when I put 'app.js'
into my website’s files? Will my index.html
form still activate 'app.js'
, as long as it’s available?
Again, still very new to this, at least the back-end stuff. Thank you for any help.
3
Answers
If anybody is curious, I just used PHPMailer to redo the whole thing (first time did it with Nodemailer). This way I don't have to deal with Node compatibility with my shared hosting service (Bluehost). I can just put the PHP file in there.
You don’t show ANY of your code which makes it a bit hard for us to guess what you’re doing.
You do have to start your web server somehow or there won’t be any server process running and nothing to respond to incoming requests.
app.listen(port)
is one such way to start the server when using Express.You don’t say what type of web hosting account you have, but it sounds like you may may be attempting to use a shared hosting account that does not specifically support node.js apps and might not be compatible with a node.js app.
You will need a hosting account that lets you have a long running server process (your node.js server process) and you will have to follow the hosting directions for how you configure the port on your server to match how their shared infrastructure works.
Good question! In essence, it is indeed necessary. Your express server is a live process that requires input by listening to a port on your server and you can host this in many ways. If this process isn’t running on your server, there would be no process running for your requests to be… well, processed!
Simply uploading it to your webserver of choice would essentially just be a JS file sitting on your webserver, very much lonely and without purpose! Here’s a nice summary to get you going on deploying an express server.