Так что я боролся с этой проблемой уже несколько дней, пробуя почти все предложения форума, с которыми мне приходилось сталкиваться. По сути, я настроил бэкэнд-сервер, чтобы позволить себе отправлять электронные письма через узел. Странно то, что он отлично работает на моей локальной машине, и он даже прекрасно собирается и разворачивается, когда я пу sh на Heroku. Но, тем не менее, мне просто предлагают «Cannot GET /».
This is my file structure:
PROJETNAME
- client
^--
^-- build
^-- node_modules
^-- public
^-- src
- node_modules
- server
^--
^-- app.js
.gitignore
- package.lock.json
- package.json
This is my package.json in the root (backend) folder:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./server/app.js",
"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build" },
"author": "Liron",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"concurrently": "^5.0.2",
"express": "^4.17.1",
"express-session": "^1.17.0",
"mongoose": "^5.8.6",
"multer": "^1.4.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"socket.io": "^2.3.0"
}
}
And this is my app.js.
const session = require("express-session");
const passport = require("passport");
const User = require("./modules/user");enter code here
const Message = require("./modules/message");
const path = require("path");
const bodyParser = require("body-parser");
const socket = require("socket.io");
const mongoose = require("mongoose");
const express = require("express");
const app = express();
//Connect mongoDB
// mongoose.connect("mongodb://localhost:27017/chatapp", {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false});
mongoose.connect("mongodb+srv://Liron:wassa@chatapp-mv7ns.mongodb.net/test?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false});
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//Passport config
require("./config/passport")(passport);
//==================================================== To check
//Express Session
app.use(session({
secret: "Rusty is the best",
resave: true,
saveUninitialized: true,
}))
//==================================================== End
//Passport middleware
app.use(passport.initialize());
app.use(passport.session());
//Requring routes
const indexRoute = require("./routes/index");
const userRoute = require("./routes/user");
const imageRoute = require("./routes/image");
const commentRoute = require("./routes/comment");
const requestRoute = require("./routes/request");
const friendRoute = require("./routes/friend");
const likeRoute = require("./routes/like");
//Routes
app.use("/users", indexRoute);
app.use("/chatapp/user", userRoute);
app.use("/chatapp/:username/:userid/image", imageRoute);
app.use("/chatapp/username/:id/image/:imageid/like", likeRoute);
app.use("/chatapp/:recipientname/:recipientid", requestRoute);
app.use("/chatapp/:username/:userid/image/:imageid/comment", commentRoute);
app.use("/chatapp/:recipientname/:recipientid/:sendername/:senderid/friend", friendRoute);
function ensureAuthenticated(req, res, next) {
if(req.isAuthenticated()) {
return next();
}
else {
//Todo send data to login form
req.flash("error_msg", "Please log in to view this resource");
res.redirect("/users/register");
// res.send("Please log in to view this resource");
}
}
const server = app.listen(process.env.PORT || 5000, function(){
console.log("server has runnig");
});
//================================================================== Message Route
//Socket setup
const io = socket(server);
const users = [];
io.on("connection", function(socket){
socket.on("username", function(data){
for(let idx = 0; idx < users.length; idx++){
if(users[idx].userEmail == data.userEmail){
users.splice(idx, 1);
}
}
users.push({userName: data.userName, userEmail: data.userEmail, socketId: socket.id, connected: true});
users.forEach((user) => {
io.to(user.socketId).emit("getUsers", users);
});
});
socket.on("disconnect", function(){
let userIndex = users.findIndex(user => {
return(user.socketId === socket.id)
})
users[userIndex].connected = false;
users.forEach((user) => {
io.to(user.socketId).emit("getUsers", users);
});
})
//Listen for events
socket.on("sendMessage", function(data) {
let message = {content: data.message};
io.to(data.recipient).emit("newMessage", {message: data.message, senderId: socket.id, senderName: data.sender});
});
socket.on("myId", function(){
io.to(socket.id).emit("myId", {id: socket.id});
})
socket.on("typing", function(data){
io.to(data.userget).emit("typing", data.usersend);
});
});
//================================================================== END
//================================================================== Cover Image Route
app.get("/images/:file", function(req, res){
const fileName = path.join(__dirname, "uploads/", req.params.file);
res.sendFile(fileName);
});
Here is my package.json in the client folder:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:5000",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"axios": "^0.19.1",
"bootstrap": "^4.4.1",
"jwt-decode": "^2.2.0",
"moment": "^2.24.0",
"react": "^16.12.0",
"react-bootstrap": "^1.0.0-beta.16",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"socket.io-client": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}