Using a node.js app with HTML - javascript

My goal is this: JS but server-side. My solution, the obvious, node.js. I've used node.js quiet a bit. Mainly for an application, not a web server. The only reason I need to do server-side JS is that I need to use a library that connects to the Discord API. So I have a little test .js file with my node.js in it. It just prints text if it works. Basic. What I need it to do is whenever someone goes to https://example.com/something, it runs the node.js script and if the script ends up with printing "hello", then https://example.com/something will say "hello".
I've done some research on this, I've found ways to deploy a node.js app, which I know how to do. I can't really find anything that I'm looking for though.

You can use express to run a webserver on nodejs
Install express by running "npm install express" in your project folder through command prompt
Create a app.js file with the following code
var express = require('express'); // load the express library
var app = express(); // create an instance of express
var child_process = require('child_process'); //load the child_process module
app.get("/something", function(req, res) { // Setup a router which listens to the site http://localhost/something
child_process.fork("./yourCodeFile.js"); // Launch your code file
});
app.listen(80);
Run node app.js to listen to web connections
Then you put your code into the yourCodeFile.js which has to be be in the same folder as the app.js file, even better you could just write all your code in the app.js code as long as you keep it inside the function inside app.get

You should take a look at cloud-based lambda functions and platforms like AWS Lambda, which run a script in response to an HTTP request. They are relatively new and the architecture used to support this is being called "serverless", which is a simple term, albeit a bit of a misnomer. There are various tools out there to help you build these systems, such as the similarly named Serverless framework, though you can typically still use more traditional server frameworks that you are probably more comfortable with. Either way, you are not responsible for managing any server, including starting it or stopping it.
In terms of constructing a response that you are happy with, you can of course respond with any arbitrary string you want. See the AWS example of a Node.js handler.
exports.myHandler = function(event, context, callback) {
callback(null, "Hello, world!");
}
Lambda functions can also return binary data and work well with static storage systems like Amazon S3. For example, the function can be run in response to the creation of static assets.

Your code should look like this:
const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const pathName =url.parse(req.url).pathname;
if (pathName == '/something') {
res.end('Hello World\n');
} else {
res.end('Please visit /something \n');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
You should run your file with node youfile.js And when you do curl http://127.0.0.1:3000 you will see
Please visit /something
But when you do curl http://127.0.0.1:3000/something you will see
Hello World

Related

NodeJs: How do I access the functions of my JavaScript backend from my HTML frontend?

Here is my HTML code in index.html.
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="stuff()">Click</button>
<script>
async function stuff() {
await connectToServer();
}
async function connectToServer() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
alert(this.responseText);
};
xhttp.open('GET', 'C:/Users/myName/myFolder/index.js', true);
xhttp.send();
return;
}
</script>
</body>
</html>
Then, here is my backend code in index.js.
const express = require('express');
const axios = require('axios');
const port = process.env.PORT || 8080;
const app = express();
app.get('/', (req, res) => {
res.sendFile('C:/Users/myName/myFolder/views/index.html');
});
app.listen(port, () => console.log(`Listening on port ${port}`));
I can type node index.js on the command line and run this program and go to http://localhost:8080/ . When I do this, the html page shows up as intended. However, when I click the button in order to make a GET request to the server side, I get a console error saying Not allowed to load local resource: file:///C:/Users/myName/myFolder/index.js . I'm using Google Chrome by the way.
I know that it is a security thing, and that you are supposed to make requests to files that are on a web server (they begin with http or https). I suppose then, my question is:
How do I make it so that my server file index.js can be viewed as being on a server so that I can call functions on the backend from my frontend?
You have to make an HTTP request to a URL provided by the server.
The only URL your server provides is http://localhost:8080/ (because you are running an HTTP server on localhost, have configured it to run on port 8080, and have app.get('/', ...) providing the only path.
If you want to support other URLs, then register them in a similar way and write a route to handle them.
The express documentation will probably be useful.
You should not need to load your server-side code into the browser. It's server-side code. It runs on the server. It isn't client-side code. It doesn't run in the browser. The browser does not need access to it.
If you want to load some actual client-side JS from the server, then use <script src="url/to/js"></script> (and not Ajax) and configure express' static middleware.
Let's improve your current flow by separating your backend API process from frontend hosting process. While backend can, it's not good in serving static html files (especially for local development purposes).
Run your backend as usual, node index.js. But as soon as this command will become more complicated, you will probably want to use npm scripts and do just npm start)
Run separate server process for frontend. Check out parcel, snowpack, DevServer. It can be as easy as npx parcel index.html, but this command is likely to change frequently with your understanding of your tool features.
To call backend, just add an API endpoint to an express app (just like you already did for serving static content), and call it, using backend process URL.
Usually, you will see your app on http://localhost/ and it should do requests to http://localhost:8080/.
If for some strange reason you will want to dynamically download js file from your server to execute it, you just need to serve this file from your frontend hosting process. In order to do so, different development servers have different techniques, but usually you just specify file extensions and paths you want to be available.
After editing frontend files, you will see hot-reload in browser. You can achieve the same for node process with various tools (start googling from nodemon)
If you find this way of operating not ideal, try to improve it, and check what people already did in this direction. For example, you can run two processes in parallel with concurrently.

How do I globally install a Node.js command-line script on the server?

As a prototype, I’ve written a simple Node.js command-line script which loads a .txt file and outputs the contents. I’ve read many articles which suggest that Node.js scripts are usually executed via the command-line and should be installed globally, so that’s the direction I’ve taken, even if there are other techniques.
For the sake of this query, my globally installed command is called my-prototype.
This all works fine on the desktop but I intend to host this prototype on a server, using Express.
How do I globally install my-prototype on the server and, if that’s possible, will I be able to execute it in the same way as in my local tests, as a child process?:
const { execSync } = require('child_process')
const output = execSync(`${process.execPath} my-prototype`)
console.log(output.toString())
I'm not entirely sure that globally installing command-line scripts is for use on a server and wonder whether that's for desktop use only.
If you need just output the contents, then you do not need a console, or rather: you only need it once, to run nodejs
const fs = require('fs');
const express = require('express');
const app = express();
//
app.get('/', async(req, res)=>{
let fileContains = fs.readFileSync('./package.json', 'utf8');
try{
fileContains = JSON.parse(fileContains);
}catch(e){}
res.json(fileContains);
});
//
app.listen(80);
Also, read a little more about the pm2 module, it's very useful

Why we need http module installed to run our node js application?

I have found so many sources for now when the first application shows this line
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
Just being geek, my Question is why we need server/port to listen our requests for our node js applications?
Why can't we run as localhost/application_name instead?
Why we need that?
Can anyone elobarate please?
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
So if you want an application which only work with bash you don't need any http modules.
Browsers use HTTP. So if you want to develop a web application you need to use that protocol. If you run your project on 80 port you can use it like localhost/my_application.
Simple app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
console.log(result);
You can call it from bash. node app.js. But it just work and stop.
But if you want to serve this structure to WWW (which is using HTTP) you need to create server. http is a great and simple module for creating servers with node.js.
You can use other js files with using require.
app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
module.exports = result;
server.js
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var result = require('app.js');
res.end(result);
}).listen(80);
Now you can run your server. node server.js
You can run arbitrary javascript with node. The code you've provided specifically sets up an http server that listens on port 8080. You can reach that webserver from a browser on the same computer by browsing to http://localhost:8080.
We don't need to install 'http' module in order to use it, it is already there in nodejs framework itself.
If you want to see output of any programming language you serve it as http because you want your browser to reach your server. Like what you do it php built in server php -S localhost:8081 or serve it via nginx or apache
If you don't serve your JS, PHP, Python ... over http, browser will treat those files as other unsupported file like a .tar file.
Node is JavaScript environment, Not a web server. You need a server to serve your application. You may use http, https or you can create any other server that can serve your js file.
Well, I do not know if my answer is clear enough to explain but hope you will have some idea why you use http module in your nodejs application.

What is the easiest way to spin up a node/js app?

I am having trouble building a node/js app with various frameworks. I keep getting the error require is not defined even though I have followed various browserify tutorials to fix it.
To give a flavour of what I want to do. I want the app to be able to be ran on a server and then I can npm install anything and these things work smoothly. I have been using express, firebase etc to handle some of my issues.
This is an extract of my app.js file:
var express = require('express');
var app = express();
var firebase = require('firebase');
app.get('/', function (req, res) {
res.sendFile(__dirname + '/html/index.html');
});
app.get('/welcome', function (req, res) {
res.sendFile(__dirname + '/html/welcome.html');
});
I have tried to use bundle.js to get require working but it still insists it is not defined.
Browserify will let you transpile some code so it will run in the browser.
It won't let you do things which are fundamentally impossible in the browser (such as running an HTTP server as you are trying to do here).
If you want to run code that requires Node JS then you need to run it through Node JS. Typically via node app.js in your command line shell.

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