What is the best way to mock ajax response with angular 2 - node.js

I have a java back-end that support all of the calls from the UI.
For the UI Phase i'm using nodeJS in order to run it and check that everything is working at localhost:4200, when need to publish it to the tomcat server, copying the dist folder to the webapp at tomcat then the application is available at localhost:8080.
The questions is if there is a simple and easy way while running the UI side on nodeJS to mock all of the response to the ajax call that been made in the UI and when publishing the dist folder to the tomcat it will be transperant and will work with the server side.
I think that its kind of setting up an additional server in nodeJS and map all of the calls and return the static json from there. or there is any other easy way?

"scripts": {
"start": "start npm run start-server && gulp serve",
"start-server": "node src/server/app.js",
"start-client": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"devDependencies": {
"express": "^4.14.0"
}
create server.js file in project folder and add below code
var express = require("express");
var app = express();
var router = require('express').Router();
var data = require('./server/api/users/data.json');
app.get("/", function(req, res) {
res.send(data)
});
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});
Create a folder for mock data naming server->api->users->data.json and put the below data
[{
"name":"Hello"
},
{
"name":"World"
}]
In order to use mock data, you need to install express server. Add above lines in your package.json and run command npm install and once install, use npm start. npm start will start your gulp serve(or use appropriate cmd as per server installed) and express server both.

Related

Backend Node.js Server not functioning in Heroku Deployment

I am having issues deploying a React App with Node.js backend server to Heroku. The app I've made allows the user to post a race score to a mongodb database, and all results can be retrieved and displayed from the database. I use two server ports when running this app locally, Localhost: 3000 for the frontend React and Localhost: 4000 for the backend node server. When running this app locally I was able to make it work by hardcoding the localhost:4000 URL for the axios calls to the database. Removing the Localhost:4000 part of the axios calls makes the calls default to localhost: 3000, which leads me to think the axios calls are not routing properly in the Heroku deployment. Furthermore when the axios calls are set to Localhost:4000 URL the Heroku app works fine if the backend server is spun up on my local machine.
I believe the issue here is the backend server is not running in the Heroku deployment, and that the routes may also need to be revised. I'm new to these deployments so any help here would be appreciated!
Backend Server:
https://github.com/Bensonm3/Max-a-thon-Results/blob/master/backend/server.js
Create Athlete Component:
https://github.com/Bensonm3/Max-a-thon-Results/blob/master/src/components/create-athlete.component.js
Deployed App:
https://max-a-thon-results.herokuapp.com/
Your application is running in development mode. You must serve your app wiht your nodejs server.
Do this refactoring
Add this code in your server.js
// server.js
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// comment this code when you run in local
And add heroku post build like this in your package.json
//package.json
"scripts": {
"start": "node backend/server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false && npm install && npm run build"
},

React.js Production Deployment that runs at the background/does not affect from windows log-off and restart

I have an react.js frontend application that is created with creact-react-app utility. I intend to run the app on a windows server machine and it has the following build/run tasks that are defined in my package.json file:
"scripts": {
"build:dev": "env-cmd -f .env.dev npm run build",
"build:test": "env-cmd -f .env.test npm run build",
"build:prep": "env-cmd -f .env.prep npm run build",
"build:prod": "env-cmd -f .env.prod npm run build",
"start-server": "env-cmd -f .env.prod node server/server.js"
}
So, for preparing my app for deployment on a windows server, I issue the following while I am inside the OS with an open command console:
npm run build:prod
npm run start-server
As a result of "npm run build:prod", the bundle is placed under my build directory in my project root. On the other hand, executing "npm run start-server" executes the following script, which is server/server.js.
const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, '..', 'build');
const port = process.env.REACT_APP_PORT;
app.use(express.static(publicPath));
app.get('*', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
console.log('Server has started listening at port ' + port);
});
Everything is fine until here, my application works but it keeps the command console busy. What I need is to run the app in the background and even if I log out windows, the app should continue to run and respond to requests. Moreover, it should start after windows restart. What would be the most convenient approach to realize this? I have read the create-react-app documentation but I did not get much help. In addition I noticed there is an npm package named pm2, but my corporate regulations dont let me install it unfortunately.
QUESTION: So, briefly I need your suggesstions on what would be the best practice approach to run my react.js front-end app at the background such that it keeps alive after log off and get restarted after windows restart? Thanks.

How can I run a node server with a next.js application?

I'm setting up a webapp in next.js that runs in parallel with a node express server which does some middleware work to interface with an API server.
I have everything working however I don't know how to make it all work together when making a npm run start. The only way it works is with a "node server.js" in one terminal and a "npm run start" in another.
I've tried to add a package.json script like this:
"start-server": "next start && node server.js"
but it only starts the next.js instance, and if I reverse the order then it only starts the node instance.
How do I make them both work so I can deploy this project?
Also since Next is a server-side package, you can build the next app to use your server.js code before deploying the server.
like so:
/* eslint-disable no-undef */
const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const options = {
...
};
app.prepare().then(() => {
const server = express();
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen('8700', err => {
if (err) throw err;
console.log(`> Ready on Port 8700`);
});
});
Install Concurrently npm package to your node dependencies.
npm install concurrently
Add the following scripts to node server's package.json
"frontend-install": "npm install --prefix frontend",
"start": "node server.js",
"server": "nodemon server.js",
"frontend": "cd ./frontend && npm run start",
"dev": "concurrently \"npm run server\" \"npm run frontend\""
i am assuming that your next application is in a folder/directory called "frontend".
3. Add the following code to package.json of the nextjs application.
"proxy": "http://localhost:5000"
Now npm run start command to start node server and nextjs app together.

how to run an angular 6 app in node server

I have a Angular 6 application which should be served by a node server running on the port 8080. I have searched and found that adding server.js as below will do the trick, yet I need to change the package.json too. I do not have any clue on how to add server.js as the start on package json file. My project is a complete Angular 6 app.
server.js is as below
var express = require('express');
var app = express();
app.use(express.static('app'));
app.get('/', function (req, res,next) {
res.redirect('/');
});
app.listen(8080, 'localhost');
console.log('app is Listening on port 8080');
package.json scripts part is as below,
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
This is the front-end and the back-end runs separately in another node server, which is a node project.
How to run Simple Application with Angular 6 + Node.js & Express; Look here: https://levelup.gitconnected.com/simple-application-with-angular-6-node-js-express-2873304fff0f

How to run Angular 2 client app and Node server app on the same host

I have built an app in Angular 2 to fetch data from a DB, and used node/express to fetch data from the server and serve it to the Angular client. Currently both of them are running in different local hosts. How do I combine them into a single project and run it on the same host?
Suppose your express site is running on localhost:3500 and all api call begins with /api
create proxy.conf.json with the content
{
"/api": {
"target": "http://localhost:3500",
"secure": false
}
}
then edit package.json and change "start": "ng serve", to "start": "ng serve --proxy-config proxy.conf.json"
now run the project using command npm start.Note:- ng serve will not work
For more information find documatation here

Resources