I have here an app that displays the location of the ISS on a map, and a user can send a message to the ISS which puts an icon where the ISS was when the message was sent.
It works wonderfully both when I start up the server and client locally and when I use the server to send the index.html page, but as soon as I deploy to Heroku, the requests to the server are failing and I cannot get the ISS location or add a message.
Here is my index.js file
const express = require("express");
const app = express();
const path = require("path");
const cors = require("cors");
const coords = require("./schema");
const axios = require("axios");
var moment = require("moment");
moment().format();
app.use(express.json());
app.use(cors("*"));
app.post("/message", (req, res) => {
axios
.get(
`https://nominatim.openstreetmap.org/reverse?lat=${req.body.lat}&lon=${req.body.long}&format=json`
)
.then(({ data }) => {
var testCoords = new coords({
lat: req.body.lat,
long: req.body.long,
text: req.body.text,
date: Date.now(),
location: data.error ? "the ocean" : data.display_name
});
testCoords.save(err => {
if (err) {
console.log(err);
} else {
coords.find({}, (err, docs) => {
res.status(200).send(docs);
});
}
});
});
});
app.get("/message", (req, res) => {
coords.find({}, (err, docs) => {
res.status(200).send(docs);
});
});
app.use(express.static(`${__dirname}/../build`));
app.get("*", (req, res) => {
process.env.PORT && res.sendFile(path.join(__dirname, "../build/index.html"));
});
app.listen(process.env.PORT || 3000, () => {
console.log("Server started");
});
and my App.js client
import React, { Component } from "react";
import { Map, Marker, TileLayer, Popup } from "react-leaflet";
import axios from "axios";
import L from "leaflet";
import Form from "./Form";
import Moment from 'moment';
export default class App extends Component {
constructor() {
super();
this.state = {
lat: "",
long: "",
zoom: 10,
messages: [],
showForm: true
};
}
componentDidMount = () => {
axios.get("/message").then(res => {
this.setState({
messages: res.data
});
setInterval(this.getIss, 2500);
});
};
postMessage = message => {
axios
.post("/message", {
text: message,
lat: this.state.lat,
long: this.state.long
})
.then(res => {
this.setState({
messages: res.data,
showForm: false
});
});
};
getIss = () => {
axios.get("https://api.wheretheiss.at/v1/satellites/25544").then(res => {
this.setState({
lat: res.data.latitude,
long: res.data.longitude
});
});
};
formatDate = date => {
return Moment(date)
._d.toString()
.split(" ")
.splice(0, 4)
.join(" ");
}
render() {
const myIcon = L.icon({
iconUrl: require("./assets/ISS-sm.png"),
iconSize: [64, 64],
iconAnchor: [32, 64],
popupAnchor: null,
shadowUrl: null,
shadowSize: null,
shadowAnchor: null
});
const messages = this.state.messages.map(message => {
return (
<Marker position={[message.lat, message.long]}>
<Popup>
<div>
<h1>{message.text}</h1>
<p>{`${this.formatDate(message.date)} over ${message.location}`}</p>
</div>
</Popup>
</Marker>
);
});
return (
<div>
<Form postMessage={this.postMessage} show={this.state.showForm} />
<Map center={[this.state.lat, this.state.long]} zoom={3}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
{messages}
<Marker
icon={myIcon}
position={[this.state.lat, this.state.long]}
/>
</Map>
</div>
);
}
}
I get a 503 error.
I have tried sending a GET to https://myherokuapp.com/messages
, but that also does not work. and I do not know how to get this thing to run properly.
Any help is appreciated.