Heroku上的Mern部署未经授权

时间:2020-01-14 06:50:05

标签: github heroku deployment mern

我创建了一个可以在localhost上完美运行的MERN应用。当我将其部署到heroku时,它会构建和部署,但是当单击该站点时,它会显示一个JSON {error:'unauthorized access'}

我确实在heroku设置中设置了环境变量,我添加了mlab add。这是我的第一个全栈应用程序,但我被这个障碍绊倒了。

这是我的server.js文件

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const dotenv = require('dotenv')
const path = require('path')


//  Configure the env file 
dotenv.config()


// Import routes
const transactions =  require('./routes/api/Transaction');
const users =  require('./routes/api/User');

// Instantiating the app variable
const app = express();


// Connecting to the mongodb server with mongoose odm
mongoose.connect(process.env.MONGODB_URI,{useNewUrlParser:true, useUnifiedTopology:true})
mongoose.set('useFindAndModify', false)


// Middleware/ Routes
app.use(bodyParser.urlencoded({extended:false}))
app.use(express.json())
app.use(cors())

app.use('/', users);
app.use('/api',transactions);


if (process.env.NODE_ENV === 'production'){
    app.use(express.static('client/build/'));
}

app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname,'client','build','index.html'));
    });

// Port for listening to requests
const port = process.env.PORT || 8000;
app.listen(port, ()=>console.log(`Server running at port ${port}`));

这是我的package.json文件

{
  "name": "expense-keeper",
  "version": "1.0.0",
  "description": "This is a full stack web app developed with React and React-Router on the front-end and Node based Express framework with MongoDB as database.",
  "main": "server.js",
  "scripts": {
    "client-install": "npm i --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "heroku-postbuild": "cd client && npm install && npm run build"
  },
  "author": "Sounak",
  "license": "MIT",
  "dependencies": {
    "@hapi/joi": "^16.1.8",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.8.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  }
}

这是heroku日志

2020-01-14T06:25:57.612892+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=b394ec75-cc8a-4e89-89a4-0a7510e722ef fwd="68.175.129.50" dyno=web.1 connect=0ms service=2ms status=401 bytes=280 protocol=https
2020-01-14T06:25:57.865746+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=0c66490c-c110-42d3-8b0e-6c8e3f997f09 fwd="68.175.129.50" dyno=web.1 connect=0ms service=2ms status=200 bytes=2543 protocol=https
2020-01-14T06:26:02.292393+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=da09827f-8f9f-4334-9d0f-e705ee8c8258 fwd="68.175.129.50" dyno=web.1 connect=0ms service=1ms status=401 bytes=280 protocol=https
2020-01-14T06:26:02.530861+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=d676d417-5944-4d1b-af04-c7bd84d383f3 fwd="68.175.129.50" dyno=web.1 connect=0ms service=2ms status=304 bytes=269 protocol=https
2020-01-14T06:30:25.931651+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=2d2a21ec-bb21-4cf1-8ba2-82df8b616135 fwd="68.175.129.50" dyno=web.1 connect=10ms service=2ms status=401 bytes=280 protocol=https
2020-01-14T06:30:26.181804+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=bb44ae1b-c4ae-4e6b-b880-2aad4e8f8e10 fwd="68.175.129.50" dyno=web.1 connect=0ms service=3ms status=304 bytes=269 protocol=https
2020-01-14T06:32:08.229662+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=3dc1106f-3cd9-4ca8-9b5f-d4d5d191ad09 fwd="68.175.129.50" dyno=web.1 connect=0ms service=3ms status=401 bytes=280 protocol=https
2020-01-14T06:32:08.520149+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=48f2c79b-746d-44f9-97c2-5a98c8d00be6 fwd="68.175.129.50" dyno=web.1 connect=0ms service=3ms status=200 bytes=2543 protocol=https
2020-01-14T06:32:09.508683+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=02b8ca31-590e-4c13-9948-8f9472933ff7 fwd="68.175.129.50" dyno=web.1 connect=37ms service=22ms status=401 bytes=280 protocol=https
2020-01-14T06:32:09.761767+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=7bf90989-7514-421d-bf46-2eb10d6df3fc fwd="68.175.129.50" dyno=web.1 connect=0ms service=8ms status=200 bytes=2543 protocol=https
2020-01-14T06:36:06.605355+00:00 heroku[web.1]: Restarting
2020-01-14T06:36:06.625473+00:00 heroku[web.1]: State changed from up to starting
2020-01-14T06:36:07.284648+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-01-14T06:36:07.345407+00:00 heroku[web.1]: Process exited with status 143
2020-01-14T06:36:18.062389+00:00 heroku[web.1]: Starting process with command `npm start`
2020-01-14T06:36:22.952272+00:00 app[web.1]: 
2020-01-14T06:36:22.952289+00:00 app[web.1]: > expense-keeper@1.0.0 start /app
2020-01-14T06:36:22.952291+00:00 app[web.1]: > node server.js
2020-01-14T06:36:22.952293+00:00 app[web.1]: 
2020-01-14T06:36:23.972491+00:00 app[web.1]: Server running at port 13431
2020-01-14T06:36:24.414222+00:00 heroku[web.1]: State changed from starting to up
2020-01-14T06:41:54.987053+00:00 heroku[router]: at=info method=GET path="/" host=mernbudget.herokuapp.com request_id=7db3280b-7d5a-4bf2-87af-0c72cfb48540 fwd="68.175.129.50" dyno=web.1 connect=1ms service=12ms status=401 bytes=280 protocol=https
2020-01-14T06:41:55.302590+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mernbudget.herokuapp.com request_id=d6438a5c-b944-41b0-be20-6e71283d0a93 fwd="68.175.129.50" dyno=web.1 connect=1ms service=17ms status=304 bytes=269 protocol=https

我被困住了。我该怎么办?我在做什么错了?

ps:我使用npm作为软件包管理器,并使用github与heroku而不是heroku-cli集成

0 个答案:

没有答案