I am using angular 5 with node js to make a crud for events data.
When I am trying to get events data from 4200 port(http://localhost:4200/event) which is running through angular is working well. It is showing the data in html with all the values. But when I am using 8080 port(http://localhost:8080/event) which one is from nodejs it is showing data only in json. No html content from event.component.html is showing here. express.js looks like this
/* ===================
Import Node Modules
=================== */
const express = require('express');
const app = express();
const router = express.Router();
const mongoose = require('mongoose');
const config = require('./database');
const path = require('path');
const appRoot = require('app-root-path');
//custom module
const event = require('../config/routes/event.router');
const bodyParser = require('body-parser');
const cors = require('cors');
const port = process.env.PORT || 8080; // Allows heroku to set port
mongoose.Promise = global.Promise;
//assigning value
process.env.NODE_ENV = 'devlopment';
/**
* Database connection
*/
mongoose.connect(config.uri, {
useMongoClient: true,
}, (err) => {
// Check if database was able to connect
if (err) {
console.log('Could NOT connect to database: ', err); // Return error message
} else {
console.log('Connected to ' + config.db); // Return success message
}
});
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(appRoot.path, 'dist')));
/**
* Routing
*/
app.use('/event', event); //Event Router
app.get('*', (req, res) => {
res.sendFile(path.join(appRoot.path, 'dist/index.html'));
});
/**
* Assiging port to server
*/
app.listen(port, () => {
console.log('Listening on port ' + port + ' in ' + process.env.NODE_ENV + ' mode');
});
my event.router.js looks like this
const Event = require('../../model/event.model');
var multer = require('multer');
var upload = multer({ dest: './public/uploads/img/',fileFilter:function(req,file,cb){
var ext = file.originalname.split('.').pop();
cb(null, file.fieldname + '-' + Date.now() + '.' + ext);
}
}).single('eventimage');
/* GET ALL EVENTS */
router.get('/', function(req, res, next) {
Event.find(function (err, events) {
if (err) return next(err);
res.json(events);
});
});
/* GET SINGLE EVENT BY ID */
router.get('/:id', function(req, res, next) {
Event.findById(req.params.id, function (err, post) {
if (err) return next(err);
res.json(post);
});
});
module.exports = router;
event.component.html looks like this
<div class="container">
<h1>Event List</h1>
<table class="table">
<thead>
<tr>
<th>Event Id</th>
<th>Event Name</th>
<th>Event Desc</th>
<th>Event Date</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let event of events">
<td><a routerLink="/event-details/{{ event._id }}">{{ event._id }}</a></td>
<td>{{ event.eventname }}</td>
<td>{{ event.eventdesc }}</td>
<td>{{ event.eventdates }}</td>
</tr>
</tbody>
</table>
</div>
event.components.ts looks like this
import { Component, OnInit } from '@angular/core';
import { EventService } from '../event.service';
@Component({
selector: 'app-event',
templateUrl: './event.component.html',
styleUrls: ['./event.component.css']
})
export class EventComponent implements OnInit {
events: Event[] = [];
constructor(
protected eventService: EventService
) { }
ngOnInit() {
this.getAll();
}
getAll() {
this.eventService.getEvents().subscribe(res => {
this.events = res as Event[];
}, err => {
console.log(err);
});
}
}
event.service.ts looks like this
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient } from "@angular/common/http";
import 'rxjs/add/operator/map';
@Injectable()
export class EventService {
domain = 'http://localhost:8080';
headers = new Headers({ 'Content-Type': 'application/json' });
constructor(
private http: Http,
) { }
getEvent(id: string) {
return this.http.get(this.domain + '/event/' + id, { headers: this.headers }).map(res => res.json());
}
getEvents(){
return this.http.get( this.domain + '/event', {headers: this.headers}).map(res => res.json() );
}
}
So can someone tell me what is the wrong here. Why I am getting data as json in 8080 port and why its showing fine with 4200 port? How I will get the data with html in 8080 port? Any help and suggestions will be really appreciable.
/eventroute in two different routers. when the pages are separated, the appropriate one is activated for the app running on that port. however, when the apps are combined into one port, the express route/eventwill always happen, and the angular/eventroute cannot happen at all./api/eventto avoid the conflict.