Как сохранить данные в удаленной базе данных (mlab), пока локальная база данных работает нормально в приложении Node.js? - PullRequest
0 голосов
/ 04 января 2019

В этом простом приложении (https://enigmatic -thicket-97026.herokuapp.com / blogs ) есть страница блога, и я храню данные в mlab и использую mongoDB в качестве локальной базы данных.Теперь, пока я сохраняю данные в моей базе данных loacl, она работает нормально, но в живом приложении я не могу этого сделать.

Я настроил файл конфигурации для использования разных баз данных для другой среды, новсе еще не работает.

Это файл конфигурации

   if(process.env.NODE_ENV === 'production'){
   module.exports={
    mongoURI:'mongodb://<dbuser>:<dbpassword>@ds125938.mlab.com:25938/blog'
  }
  }else module.exports={
  mongoURI:'mongodb://localhost/blogpage'
  }

Это код

const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const mongoose= require('mongoose');
const about = require('./routes/about');
const blogs = require('./routes/blogs');
const posts = require('./routes/posts');
const works = require('./routes/works');
const app = express();

const db = require('./config/database');

console.log(`app: ${app.get('env')}`);


app.use('/about', about);


app.use('/blogs', blogs);

app.use('/posts', posts);

app.use('/works', works);

//Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())



// mongodb connection

mongoose.connect(db.mongoURI,{

useNewUrlParser: true 
})
.then(()=>console.log('connected to mongodb..'))
.catch(err=>console.error('unable to connect',err)); 


//load blog model 

require('./models/blogs');

const Blog = mongoose.model('blog');



app.get('/',(req,res)=>{
res.render('index');
})

//Form submit and redirect to post page

app.post('/blogs',(req,res)=>{

let errors = [];

if(!req.body.title){
errors.push({
    text:'please add some title'
})
}
if(!req.body.description){
errors.push({
    text:'please add some description'
})
}
if(errors.length > 0){
res.render('blogs/blogs',{
    errors:errors,
    title:req.body.title,
    description:req.body.description
});

}else{
//console.log(req.body);
new Blog(req.body)
.save()
.then(blog=>{
    res.redirect('/posts');
    console.log(blog);
})
.catch(err=>console.error(err)); 

}


});



//static files

app.use(express.static(path.join(__dirname,'public')));







//express-handlebar middleware

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');



const port = process.env.PORT || 8000;
app.listen(port,()=>{
console.log(`server started at port ${port}`);
});

Я использую heroku cli, и этоя получаю ошибку

    2019-01-03T17:18:00.000000+00:00 app[api]: Build succeeded
    2019-01-03T17:18:06.325718+00:00 app[web.1]:
    2019-01-03T17:18:06.325738+00:00 app[web.1]: > blog@1.0.0 start /app
    2019-01-03T17:18:06.325740+00:00 app[web.1]: > node index.js
    2019-01-03T17:18:06.325741+00:00 app[web.1]:
    2019-01-03T17:18:07.297676+00:00 app[web.1]: app: production
    2019-01-03T17:18:07.322566+00:00 app[web.1]: server started at port 12809
    2019-01-03T17:18:07.331209+00:00 app[web.1]: unable to connect {             MongoParseError: Unescaped at-sign in authority section2019-01-    03T17:18:07.331217+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb-core/lib/uri_parser.js:450:21)
2019-01-03T17:18:07.331219+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3)
2019-01-03T17:18:07.331221+00:00 app[web.1]: at connectOp (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
2019-01-03T17:18:07.331222+00:00 app[web.1]: at executeOperation (/app/node_modules/mongodb/lib/utils.js:420:24)
2019-01-03T17:18:07.331224+00:00 app[web.1]: at MongoClient.connect     (/app/node_modules/mongodb/lib/mongo_client.js:168:10)
    2019-01-03T17:18:07.331241+00:00 app[web.1]: at Promise     (/app/node_modules/mongoose/lib/connection.js:521:12)
2019-01-03T17:18:07.331243+00:00 app[web.1]: at new Promise (<anonymous>)
2019-01-03T17:18:07.331246+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:518:19)
2019-01-03T17:18:07.331247+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:270:15)
2019-01-03T17:18:07.331249+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:33:10)
2019-01-03T17:18:07.331250+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:689:30)
2019-01-03T17:18:07.331252+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
2019-01-03T17:18:07.331254+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:599:32)
2019-01-03T17:18:07.331255+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
2019-01-03T17:18:07.331257+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:530:3)
2019-01-03T17:18:07.331258+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
2019-01-03T17:18:07.331260+00:00 app[web.1]: name: 'MongoParseError',
2019-01-03T17:18:07.331262+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {} }
2019-01-03T17:19:49.093655+00:00 heroku[router]: at=info method=GET path="/css/style.css" host=enigmatic-thicket-97026.herokuapp.com request_id=c6387ba4-ad18-48c3-9098-f471d0603162 fwd="223.191.8.24" dyno=web.1 connect=1ms service=12ms status=200 bytes=1312 protocol=https
2019-01-03T17:19:50.119357+00:00 heroku[router]: at=info method=GET path="/img/hero2.jpeg" host=enigmatic-thicket-97026.herokuapp.com request_id=5a60ecd4-3a3c-4629-abf0-ea583790d5f7 fwd="223.191.8.24" dyno=web.1 connect=1ms service=8ms status=200 bytes=70085 protocol=https
2019-01-03T17:19:53.865891+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=enigmatic-thicket-97026.herokuapp.com request_id=22fc17fd-940e-4231-ab0b-3dcb08199e2e fwd="223.191.8.24" dyno=web.1 connect=1ms service=5ms status=404 bytes=394 protocol=https
2019-01-03T17:20:11.859071+00:00 heroku[router]: at=info method=GET path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=6c70ab4a-c419-4627-91a0-dda5e6ffc998 fwd="223.191.8.24" dyno=web.1 connect=0ms service=8ms status=200 bytes=2318 protocol=https
2019-01-03T17:20:57.065504+00:00 heroku[router]: at=info method=GET path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=c65b9442-e06f-4175-9b21-8adbaa9433c2 fwd="223.191.8.24" dyno=web.1 connect=1ms service=8ms status=304 bytes=151 protocol=https
2019-01-03T17:21:06.253102+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/posts" host=enigmatic-thicket-97026.herokuapp.com request_id=a148f338-441e-4224-bf07-7e1f745bc4a5 fwd="223.191.8.24" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2019-01-03T17:21:36.126663+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=e355f9f7-9591-47d9-aee5-79041c5cf8ef fwd="223.191.8.24" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2019-01-03T17:23:02.506788+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=fac0796c-7992-4cd6-98fb-c197d49719fa fwd="223.191.8.24" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2019-01-03T17:35:35.771633+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/posts" host=enigmatic-thicket-97026.herokuapp.com request_id=41956c51-a5e7-4e33-801f-7329e0092ba7 fwd="223.191.8.24" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2019-01-03T17:38:53.229375+00:00 heroku[router]: at=info method=POST path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=15151ac7-4a56-46b2-9452-1719e6217d2b fwd="223.191.8.24" dyno=web.1 connect=1ms service=3ms status=200 bytes=2318 protocol=https
2019-01-03T17:39:38.535300+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/blogs" host=enigmatic-thicket-97026.herokuapp.com request_id=d5f79060-7600-461f-907f-6fc3ae2572e9 fwd="223.191.8.24" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2019-01-03T17:40:44.631781+00:00 heroku[router]: at=info method=GET path="/" host=enigmatic-thicket-97026.herokuapp.com request_id=8e36c952-4c8d-4e12-a35c-68653f5abc0d fwd="223.191.8.24" dyno=web.1 connect=0ms service=2ms status=304 bytes=151 protocol=https
2019-01-03T17:40:45.011299+00:00 heroku[router]: at=info method=GET path="/css/style.css" host=enigmatic-thicket-97026.herokuapp.com request_id=7c7db2a3-b2d8-4597-922a-3fc87a7c8e7c fwd="223.191.8.24" dyno=web.1 connect=0ms service=2ms status=304 bytes=237 protocol=https
2019-01-03T17:40:57.511022+00:00 heroku[router]: at=info method=GET path="/css/style.css" host=enigmatic-thicket-97026.herokuapp.com request_id=d7251206-daf0-4cd2-8700-e52593b333f9 fwd="223.191.8.24" dyno=web.1 connect=1ms service=2ms status=304 bytes=237 protocol=https
2019-01-03T18:17:42.122641+00:00 heroku[web.1]: Idling
    2019-01-03T18:17:42.127290+00:00 heroku[web.1]: State changed from up to 
    down
    2019-01-03T18:17:43.011075+00:00 heroku[web.1]: Stopping all processes 
    with 
    SIGTERM
    2019-01-03T18:17:43.106051+00:00 heroku[web.1]: Process exited with 
    status 
    143
    2019-01-04T04:16:27.088167+00:00 heroku[web.1]: Unidling
    2019-01-04T04:16:27.091710+00:00 heroku[web.1]: State changed from down 
    to 
    starting
    2019-01-04T04:16:30.562486+00:00 heroku[web.1]: Starting process with 
    command 
    `npm start`
    2019-01-04T04:16:34.857338+00:00 app[web.1]: app: production
    2019-01-04T04:16:34.875357+00:00 app[web.1]: server started at port 29925
    2019-01-04T04:16:34.881177+00:00 app[web.1]: unable to connect { 
    MongoParseError: Unescaped at-sign in authority section2019-01- 
    04T04:16:34.881186+00:00 app[web.1]: at parseConnectionString 
    (/app/node_modules/mongodb-core/lib/uri_parser.js:450:21)
    2019-01-04T04:16:34.881188+00:00 app[web.1]: at connect 
    (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3)
    2019-01-04T04:16:34.881189+00:00 app[web.1]: at connectOp 
    (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
    2019-01-04T04:16:34.881191+00:00 app[web.1]: at executeOperation 
    (/app/node_modules/mongodb/lib/utils.js:420:24)
    2019-01-04T04:16:34.881193+00:00 app[web.1]: at MongoClient.connect 
    (/app/node_modules/mongodb/lib/mongo_client.js:168:10)
    2019-01-04T04:16:34.881195+00:00 app[web.1]: at Promise 
    (/app/node_modules/mongoose/lib/connection.js:521:12)
    2019-01-04T04:16:34.881198+00:00 app[web.1]: at new Promise (<anonymous>)
    2019-01-04T04:16:34.881204+00:00 app[web.1]: at 
    NativeConnection.Connection.openUri 
    (/app/node_modules/mongoose/lib/connection.js:518:19)
    2019-01-04T04:16:34.881206+00:00 app[web.1]: at Mongoose.connect 
    (/app/node_modules/mongoose/lib/index.js:270:15)
    2019-01-04T04:16:34.881208+00:00 app[web.1]: at Object.<anonymous> 
    (/app/index.js:33:10)
    2019-01-04T04:16:34.881210+00:00 app[web.1]: at Module._compile 
    (internal/modules/cjs/loader.js:689:30)
    2019-01-04T04:16:34.881212+00:00 app[web.1]: at 
    Object.Module._extensions..js 
    (internal/modules/cjs/loader.js:700:10)
    2019-01-04T04:16:34.881214+00:00 app[web.1]: at Module.load 
    (internal/modules/cjs/loader.js:599:32)
    2019-01-04T04:16:34.881216+00:00 app[web.1]: at tryModuleLoad 
    (internal/modules/cjs/loader.js:538:12)
    2019-01-04T04:16:34.881218+00:00 app[web.1]: at Function.Module._load 
    (internal/modules/cjs/loader.js:530:3)
    2019-01-04T04:16:34.881220+00:00 app[web.1]: at Function.Module.runMain 
    (internal/modules/cjs/loader.js:742:12)
    2019-01-04T04:16:34.881240+00:00 app[web.1]: name: 'MongoParseError',
    2019-01-04T04:16:34.881242+00:00 app[web.1]:     
    [Symbol(mongoErrorContextSymbol)]: {} }
...