how to run an angular 6 app in node server - node.js

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

Related

Nestjs/swagger open browser automatically like in REACT

I'd like my Nestjs/swagger application to start up as soon as bootstrap is finished
Initially I thought of using the callback of
async function bootstrap(): Promise<void> {
console.clear();
console.log("Starting and validating");
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
});
await app.listen(PORT, () => someOpenBrowserFuncion("/docs")`));
}
bootstrap();
But I didn't find anything like that, so I thought
When we start a REACT app, as soon as it is compiled, it opens the default browser automatically.
and
This option can be disabled with the following command:
"scripts": {
"start": "env BROWSER=none react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Is there a similar function in the Nestjs/swagger framework?
"scripts": {
"build": "nest build",
"dev": "nest start --watch",
"start": "env BROWSER=true nest start",
"production": "node dist/main",
},
Or some configuration to launch browser on certain endpoint?
You can install a npm package cross-env : npm install cross-env
And update this command in the package.json file under 'scripts';
"start": "cross-env BROWSER='chrome' nest start"
BROWSER is an environment variable, and you can use the cross-env package to properly handle it.
Linux:
BROWSER='google-chrome-stable'
Windows:
BROWSER='chrome'
OS X:
BROWSER='google chrome'
If these don't work, you can update the script:
Windows:
"start": "start http://localhost:3000 & nest start"
Mac:
"start": "open http://localhost:3000 && nest start"
Linux:
"start": "xdg-open http://localhost:3000 && nest start"
You can change your own port number.

Nodejs Ionic-Angular deployed to Heroku displays only the backend, instead of launching the whole app

My package.json:
"scripts": {
"ng": "ng",
"start": "node src/app.js && ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"main": "src/app-routing.module.ts"
},
I moved the devdependencies to dependencies.
Apps run locally with Heroku local web but display the only backend in the server.
Added a procfile containing web: npm start
Heroku assigns a port and the database is successfully connected. Backend works fine
My project structure: A src/ folder that has both my back end and frontend files.
I guess I am losing out on the project structure. Please help to make me display the frontend of the app instead of backend.
okay , 1)add the following to your app.js (or your server.js), and
app_path ='../www';
app.use('/api', songRoute)
app.use('/',express.static(path.join(__dirname,app_path)))
app.get('*',(req,res)=>res.sendFile(path.join(__dirname,app_path + '/index.html')))
2)To package.json add this,
"heroku-postbuild":"ng build --configuration=production",

front-end doesn't work when deploy the mern stack to heroku

The client-side is build with create-react-app and the backend is with the express, node.js, and MongoDB. It works locally, but when I deploy to Heroku just the backend works...
index.js
app.use(express.static(path.join(__dirname, 'client', 'build')));
+app.get('/', function(req, res) {
-app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
});
}
package.json in the server side
"scripts": {
"start": "node index.js",
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
},
package.json in the client side
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000",
also, my mongoose on MongoLab works fine and add it to Heroku
BTW, when I push to Heroku it seems everything works fine. I mean it navigates to client-side and installs and build the package
moved the app.use(express.static(path.join(__dirname, 'client', 'build'))); before the app.use(error) and error handlign function then it works

Autostart node.js app with startup options

I would like to autostart my node.js application and server with every boot of the Windows Server 2016 OS.
I already found "node-windows" and "qckwinsvc" as possible solutions, but I do not understand how I can start my application with the startup options I saved in the package.json provided below (e.g. host, prod, ip, max-old-space-size, etc.)
Furthermore, I would like to know if these services also restart the node application in case it crashed due to a programmatical error (e.g. javascript heap out of memory)
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod --base-href ./",
"build-dev": "ng build --base-href ./",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"app": "ng serve --host=0.0.0.0 --prod --open",
"app-dev": "ng serve --host=localhost --open",
"server": "export NODE_ENV=production && export IP=172.28.0.19 && node --max-old-space-size=4096 server/server.ts",
"server-dev": "export NODE_ENV=development && export IP=localhost && node server/server.ts",
"static": "http-server ./dist/lead -p 8080 -a 0.0.0.0 -o",
"static-dev": "~/.node/lib/node_modules/http-server/bin/http-server ./dist/lead -p 4200 -a localhost -o",
"format": "tslint --fix \"./src/app/**/*.ts\" && tslint --fix \"./server/**/*.ts\" && prettier --write \"./src/app/**/*.{ts,json,css,html}\" && prettier --write \"./server/**/*.ts\" && ng lint"
},
The perfect solution would be to autostart the server and application with all provided startup options and also restart the application in case of a crash.
Would be very thankful for any kind of help :)
dotenv is a popular library for environment params management. You can start your app like this NODE_ENV=prod node app.js and can access this value from your app like this process.env.NODE_ENV.

What is the best way to mock ajax response with angular 2

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.

Resources