I am going to learn Node.js and right now I was trying to make a small demo project which implements an HTTP REST service.
My idea is to divide the APIs (url) by Resources and end up in a structure like the following one:
- user
> index.js
> post.js
> put.js
> delete.js
> functions.js
- person
> index.js
- index.js
So, if you are familiar with ASP.NET Web Api, I am trying to make every module of Node.js a Controller and every web method a single file (.js), in order to have an high maintainability in the future.
Question
Right now, my index.js file return the following:
var http = require('http');
var server = http.createServer();
How can I configure a specific "Request Handler" in each file by using this module? At the moment the createServer() method return a server object can use a single server.on('request', function) while I need to handle each request in a different file.
Go ahead learning with the help of some framework. They provide scaffolding of project.
If you are developing a complete web app(MVC) then go for ExpressJs or SailsJs
If you are looking out to develop only API(No Views) then go for Strongloop or Restify
There are many more frameworks but the above ones are popular.
Related
I have a web app that has a separate spring boot backend and a nodejs frontend.
is it possible to make them both run on the same bundle, like building a singe executable? or do i need a stand alone service for each? Both are having their own jenkins pipelines and git repositories.
My backend server is just a API that provides the frontend with simple data via REST services. If it is possible what would be the way to do it?
A nodejs is a backend technology, not frontend.
You can use a nodejs as an entry point to a spring boot application but it is a code running on the server side. So nodejs is not just a list of static files as it happens for a real front end developed in react, angular or vue (or vanilla javascript). It has also an engine to run the code.
So is not possible to run nodejs server using files that are compressed in the executable jar. Instead is possible to put a real front end application (react, angular...) adding static files in the directory /resources/static
It was maybe a little missleading. My frontend is an app runing on node.js server and may backend is a java app runing on spring boot server. They are communicating on localhost via Rest.
I want to create a single entry point that starts the node.js server (I call this unit frontend) and the spring boot server ("backend") at the same time. So it feels like a self contained system.Currently the spring boot app is delivered as windows executable.
I am making a web app that uses a Node.js/Express backend and a React.js Frontend. Right now I have my Node.js code that validities login/registration credentials in a separate folder running on localhost:5000 while my React.js frontend login/registration form code is running on localhost:3000.
My question is would it be easier to put both my frontend and backend code into one file structure so I don't have to send the data from my form to my backend running on another domain so for example both my Node.js and React.js code would be running on localhost:3000 and would just send the form information to the Node.js files that validate the user credentials instead of having to use a fetch method to send my form data to the backend. Is this even possible?
Yes, it is possible, you can use Express to serve your static files (frontend ReactJS) and also your backend routes. Take the following Github repository as an example.
https://github.com/Bikranshu/express-react-boilerplate
That is possible, A monolithic architecture with express and reactjs (https://medium.com/#sesitamakloe/easy-express-react-monolith-chapter-1-a1567c6ff483)
I am trying to create a react-redux app using node server as backend. Is it possible to make the node server serve the react-redux app instead of running react-redux using dev server a=in one port and node on another port?
Need some idea to start with. Thanks in advance:)
Yes.. it is
you can serve you react app on '/'
and listen for API request in another route
so you don't have separate codebase for the react app and the api backend code
You can use express to serve the react app on a particular route
i.e my-app.com/
then serve backend related content on another route
i.e
my-app.com/api
so when a request is made to my-app.com/ express serves express serves backend resource or API
There are a few steps I take when creating an express/react app together. I'll create a server and a client directory. The client dir is created with create-react-app and the server can be created via express-generator for example. My project dir (the one that contains both of them) is basically just glue that melds the two together. In the client app, I'll add proxy:localhost:3001 (or whatever port your express api is running) and I use concurrently to run both servers (client and server - as client is being run by webpack-dev-server) at the same time. They run as separate servers during development, but when I make an api call, it's as if I'm making it directly to the express server itself.
The only other thing to worry about is deploying the application. You can use the build command that comes with create-react-app and copy that over to the public directory in your express application that is served up via express.static.
Here's a quick example to take a look at:
https://github.com/overthemike/heroku-skeleton
This is an useful doc for redux SSR setup. This helps avoiding running client and server at two ports.
Redux SSR
I am trying to learn how to use Node.js and web sockets to create simple multi-user interactive javascript programs. I used this tutorial series by Daniel Shiffman to create this example project. My next step would be to upload it, using WinSCP, to my RaspberryPi apache2 web server, but I haven't found a way to edit the code in a way to allow for that to work, and furthermore, I do not know what piece of the programs to execute to make it function properly.
Any assistance would be great. The extent of my Node / Socket.io knowledge comes entirely from the video series mentioned above, so you can assume I know almost nothing else.
Apache is a web server and it serves your file and send them to client for you, so when you have some client side things like html site with some css, javascript and images you can use apache to send them to client for you.
In node.js you can create this web server simply by following code and express library:
// Create the app
var app = express();
// Set up the server
var server = app.listen(3000, () => {
console.log('http server is ready')
});
as you created in your code too. by this web server you can host your files and do many more things like setup socket.io server and ... because you write web server yourself. with following code you serve static files in public directory (html, css, javascript and images ...):
app.use(express.static('public'));
after you finishing this process you can run it simply by:
npm install
node server.js
if you want you can run you code inside docker by creating Dockerfile and ...
About your question, you must move all your project files into raspberry and at the end you have following directory tree in somewhere in raspberry:
|- server.js
|- package.json
\ public
at this directory run above commands and your server will be up and running and you can access to it by http://raspberry_ip:3000.
This must be an extremely common problem. I've seen various answers for this but none seem to work for me.
I have node installed on an apache server on Windows Azure. My app is built and ready to go (snippet below):
var express = require("express");
var app = express();
//example api call
app.get("/api/example", function(req, res){
//do some process
res.send(data);
});
app.listen(8080);
console.log("App listening on port 8080");
Now, when testing on my own computer, I could then go to localhost:8080, which works great. But now I've put it on the azure server I can't get an external domain to point to it properly. So for example, I have the domain:
framework.example.com
I've added this to my hosts file in Azure:
XXX.0.0.01 framework.example.com
Initially I tried also editing the http-vhosts.conf to point the domain to the correct directory. This worked for loading the frontend, but the app couldn't talk to the backend. API calls returned 400 not found errors.
I've also tried an Express vhost method but think I'm doing it wrong and don't fully understand it. What is the correct method?!
My app structure is like this:
- package.json
- server.js
- server
- files used by server.js
- public
- all frontend files
So to boot the server I run server.js which runs the code at the top. The server.js uses the below Express config to point to the public folder.
app.use(express.static(__dirname + "/public"));
Adding it to the hosts file in Azure won't help. You'll need to configure your domain's DNS to point to Azure. I'd recommend using the DNS Name of your Cloud Service instance. Your underlying VM IP address could change if you need to stop it for some reason, but your Cloud Service DNS name is configured to always route to your underlying VMs. That means you'll need to setup a CNAME with your DNS.
Read more about that here: Cloud Services Custom Domain Name
Next, you'll either need to host the node app on port 80, or put a proxy in front of it to handle that for you. Otherwise you'll be stuck typing framework.example.com:8080 which is not ideal. On linux, you'll likely need to be a privileged user to host on port 80, but you never want your node app to have root privileges. You can use authbind to work around this problem.
See an example of how to use it with node here: Using authbind with Node.js
All that being said, it seems like you're somewhat new with linux server management. If that's the case, I'd strongly recommend trying to use something like Azure Websites instead of a VM. You no longer have to manage the virtual machine OS. You simply tell it to host your application and it takes care of the rest. If you're using github, this is incredibly easy to test and iterate with. It does host on Windows under the hood, and that might problems for some applications, but I host all my node sites there (developed on Mac) without any issues.