How to Setup API Environment in Node - node.js

I'm building a Node.js REST API. I have a repository which will hold this REST API code.
I want to be able to test my API. For now I'll just test via Express and make GET requests to start but I want my express stuff in a separate project because a REST API isn't gonna have any web app portion.
I'm using Webstorm. So right now I have created a gitHub repo to hold the API which I'll expose REST endpoints.
I want to create a new project also again that will be a Node-Express project for the purpose of pulling in the REST API and consume it. I am also doing this in a seperate project because I don't want my Express stuff checked into my REST API repo, it doesn't belong there. I am just using Express for testing stuff.
I realize I can later test my REST endpoints headless, but right now I just wanna get started and figure that out later and just going with an Express project to somehow consume my other API REST project.
I'm not quite sure on a few things, as I come from a .NET enviornment and no longer a .NET developer. Here are some questions I have:
My Question: If I have my Node Express project open in Webstorm, how would I "include" or "require" my rest-api? Would I somehow npm it down? But my REST api is not public. Would I just branch my git REST API repo down, then what? How would I include it into my seperate Express App project?
Or...do people usually still add express to their REST API projects anyway, maybe it doesn't matter?

You can use npm link to link a local package
$ cd main-project
$ npm link ../path/to/web-api
Then in your main project, you can require it as you would any other module
If you don't intend on publish it, the package.json in your web-api should have
{
"private": true
}

Related

asp.net core webapi with authentication (back-end) and react redux app(front-end) with authentication working independently

i need asp.netcore web api with authentication (ApplicationDbContext i.e.identity default database context ). i feel comfortable with visual studio as back-end.
2.i need react-redux app with authentication(front-end) working independently that start with "npm start". i feel good with vs code. on the front-end i want to add react-redux and redux-thunk to implement my course website front end. i have been in struggle for a week to achieve this.i tried with vs templates but when ever i install react-redux the user management fails and the client app does not start.tried, web application,api and web application with restful templates.
if someone have better idea to accomplish my task . my overall problem is to create a website that can present online courses.the teachers could add course material and student can access.
Try running npm install before your create-react-app script.
If that doesn't work, deleting "node_modules" folder and running npm install.
If that doesn't work, you can always try setting up your react environment manually (For example, as in this great tutorial here)
Worst-case, you can always use react just client-side only, and although this becomes difficult due to most online tutorials being server-side, at least you can get started and not be blocked on your project.
Source: I googled ;)

Can we use angular project and that will use in my two different API backend projects (i.e. node and .net core)?

For an example, I have one angular ui project that fetches values and perform operations.
I have one backend API project in .Net Core and now I want to reuse angular ui project with node js.
Is it possible?
How to configure it and which are the main points to take care that angular project support both.
How can I deploy it?
Angular is a client side application that run's in clients browser, as long as the REST API or service you connect has Same API endpoints and same object models and authentication etc everything will work as intended.
REST is platform independent like the web services and also language independent. It doesn't matter if you use .Net Core or some nodejs framework.
Once the angular application is build ( ng build --prod) you get a bundled application files in dist folder you can host these files in any web sever just like you host normal html file.
The only change you have to make in the the Angular application will be to change the host URL (if there is any change) normally configured in environments folder
Take help of environment file and change the api as per your requirement.

Should I build server-side of application inside nuxt.js server directory?

I want to build full-stack application with Nuxt.js. I am wondering where I should create my server-side inside Nuxt.js or maybe I should create separated project only for server stuff.
I am trying to set up my project but I do not know how I should do it. The application which I am building will have own front-end, back-end and also database (I will use MongoDB) but actually I do not know how I should start. I was reading a lot about SSR and Nuxt.js seems really good if am planing to use Vue.js on fronted. While creating nuxt app I can choose to use Express and then I can see server directory inside my directory structure does it mean that i should build all back-end inside this directory or maybe it is only for small stuff?
I have also another question what if I want to use Nest.js on back-end can i just use npm i -g #nestjs/cli and then nest new project-name inside my server directory ? I was looking also for this answer but almost all results in google for this type are about (comparison between Nuxt.js, Next.js and Nest.js).
It will be my first bigger full-stack project and I want to do it right but I am a really beginner in this so I am looking for answer from more experienced programmers.
You can run express or any node.js server you want inside Nuxt.js. When installing Nuxt.js with scaffolding tool create-nuxt-app, you can choose integrated server-side frameworks : Express, Koa, Hapi, Feathers, Micro, Fastify, Adonis (WIP). There isn't offical Nest.js integration, but you can easily find a starter kit on github.
With create-nuxt-app, if you choose to use any node.js server inside your nuxt app, you will see a server directory inside your directory structure, with the corresponding server-side pre-configured index.js file.
Here is my own feeling about it:
I think the inside solution make sense for a small SPA or Headless project (Ex: parse and serve files, a simple JWT Authentification, a small websocket server...), or for a front-end logic application that cannot fit in client browser and who are nothing to do with the database (like image or file computation).
But generally, this server run the database layer for your Nuxt application: a REST or GraphQL API. It can also run your business logic of your app, serve authentification, and more and more when project growth...
If you think about separation of concerns and microservices
architecture, do not use server inside Nuxt.js. Splitting both frontend and server will result more flexibility. You can host frontend and API in different servers.
So now, do nuxt.js really need a node.js server ?
Yes if you plan to use it in SSR mode, No if you plan to use it like a SPA or Static generated way. Docs here... .
In SSR mode, nuxt.js ask data to your API at the first rendering, and provide a complete SEO compatible page to the client browser or bots. It also provide all javascript that the browser need to navigate and fetch your API. For that, nuxt.js in SSR mode should run with node.js.
I assume you said "back-end" for your API and your business logic application, in this case, you should separate nuxt.js and your server. Two node.js instances to run both.

Basic Question First Node+express+angular app in production

I am creating my first app node express app with angular 7 on the frontend to be deployed in production. I have below question?
What folder structure is preferred, should I create separate
projects for node and angular or same project(server.js in the root
of angular project and server folder to create express server
files)? What is the preferred one and I have to checkin the project
in one folder of svn.
Should I use babel and create the node server code with es2015 or
continue with old approach?
Its all up to you, what I am doing is I have sepreate directory for Angular and Node projet
project
|
client - Your anguar project
server - Your Apis and server side coding (Only this folder require at productino level)
Then we can create a gulp file and task to gulp that Build my client
project and put that build folder inside the
server -> public
Now only server can be use to production where Build will be render as static.
And next to authentication and autherization process you can follow JWT based permission .
Generally I would say that separating your client and server code into separate projects is preferred so that you do not have to release both your client and server at the same time when you make a change to one or the other. The rest of my answer is based on the assumption that you would separate the two sides into different projects.
As far as structuring your server side Express-based application, check out this link for some guidance on how to handle your situation. See the answer to the first question about different approaches to how to structure your Express application for different deployment scenarios. Also, if you use the latest LTS version of node, you will not need to use a transpiler to convert your files to Javascript because the Node environment will handle that for you.
As far as structuring your client side Angular-based application, check out this link for a very detailed discussion about best practices for structuring your Angular application.
I would prefer following, in case in future you need to separate the API layer with client you can do it with ease,
project
|----client
| ---client-template //All UI code like .css/htmls and node process initiates from here
| ---client-angular // All the directives and controllers goes here
| ---client-service //Service layer, All the API call to server goes here
|----server
| ---server API's // separated by its own module if any
|--- you API modules and so on..
This will help you to have flexibility over client and server integration without any tight coupling. Also easy to maintain and debug.
Answer 1: you should make two separate folder/repository structure for frontend and backend.
let's suppose your application grows fast at that time you want to scale your backend and you want to host your Angular app as static web app using Amazon-S3 so at that time it will be very easy to manage this.
May you want to use CICD, in that case also it will be good if your separate folder so you can create separate CICD jobs for backend and frontend.
May be your company hired some developer which is either expert in frontend or in backend only. in that case your company don't want give them unnecessary code access. so separate repo will be an easy option for this case. (this may be Depends on your team and company's approach for development)
Answer 2: I recommend go for es6 or es6+ features.
latest node.js version is supporting some of the features of es6. for example
- spread operator
- destructing
- classes (you can use OOPs)
- arrow functions
- let, const
- async await and etc
you can use babel if any other feature which is not supported by node.js. there could be may reason for using babel, but i want to know which specific feature do you want to use with babel? so i can explain according to that.
I have used the following approach that bind the Angular Application and the Node server as a single unit.
Steps for creating the project structure is:
Create a new Angular project with the CLI.
Create a server.js file in the root directory of the project and configure it to render the contents of the dist/ folder on the / route.
You can refer the link for the server code: https://github.com/nikhilbaby/node-server
Running the server
I usually run the project with ng build && node server. This will make sure that the angular application is build first and after that node server is started.

What's the best way to lay out a React/webpack project with an API backend?

I'm a little late to the party, but I'm just getting started with React. I have a base project that I'm serving with webpack-dev-server, but now I need to actually get data for it.
My chosen API is Strava. I'm trying to use the recommended API wrapper for node, but I've come to a point where I don't want to expose my API keys and know that I need to create my own API endpoints. I was about to start creating an Express server to quickly create these endpoints, but I feel like there might be something that I'm missing.
Is there a better way to go about this with webpack/webpack-dev-server?
If I want this working on the internet, I know that I'll need at least one node instance running to serve my API endpoints; I just want to make sure there's not some super-simple, possibly webpack-based (to keep everything in the same family) solution that I could then, later, just deploy on a ready-made node server.
My assumption is that I'll just be better off creating two projects:
Static content (currently webpacked and served with webpack-dev-server for development)
Express server for API-wrapping.
If that's the case, is there a simple way to point hook Express up with my static project (everything webpack does is sent out to an /app/dist directory) for production deployment?

Resources