Error 503 Service unavailable Uploading base64 to mysql BLOB



几天来,我一直有问题,试图让我的Node.Js应用程序部署在heroku上,以保存base64字符串到Mysql数据库作为BLOB。它提示CORS错误503问题,因此我在修复中,不能再做任何事情。我必须等待几个小时,然后我才能做任何事情,我仍然无法将base 64字符串保存到Mysql (ClearDB)

这是我的后端Node.JS的样子

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');
var port = process.env.PORT || 8080;
app.use(allowCrossDomain);
app.all('*', function(req, res, next) {
var origin = req.get('origin'); 
res.header('Access-Control-Allow-Origin', origin);
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
app.get('/api/',function(req , res){
return res.send({error: false,message: 'hello'})
});
var dbConn = mysql.createPool({
host: 'xxxxxxxxxxxxxxxxx',
user: 'xxxxxxxxxxxxxxx',
password: 'xxxxxxxxxxxxx',
database: 'XXXXXXXXXXXXXXXXX'
});
// dbConn.connect();
module.exports = dbConn;
//Functions
//register
app.post('/api/register',function(req,res){
var postData = req.body;
dbConn.query('INSERT INTO icloneusers SET ?', postData, function (error,results,fields){
if(error) throw error;
return res.send({error:false, data: results, message: 'OK'});
});
});
//login
app.post('/api/login', function(req,res){
var username = req.body.username;
var password = req.body.password;
dbConn.query('SELECT * FROM icloneusers WHERE username = ? AND password =?', [username,password], function (error, results, fields){
if(results.length > 0){
return res.send({error:false, message: 'OK'});
}else{ 
return res.send({error: false, message: 'Incrorrect Login Details'});
}
});
});
app.post('/api/posts',function(req,res){
var postData = req.body;
dbConn.query('INSERT INTO icloneuserspost SET ?', postData, function (error,results,fields){
if(error) throw error;
return res.send({error:false, data: results, message: 'OK'});
});
});
app.get('/api/posts/:username', function(req,res){
let username = req.params.username;

if(!username){
return res.status(400).send({error: true, message: 'Please provide username'});
}
dbConn.query('SELECT * FROM icloneuserspost WHERE username =?', username,function(error, results, fields){
if(error) throw error;
return res.send({ error:false, data: results, message: 'posts.' });
});
});

app.post('/api/createpost',function(req,res){
var username = req.body.username;
var base64str = req.body.base64str;
var post = req.body.post;
dbConn.query('INSERT INTO icloneuserspost (username, base64str, post) VALUES (?,?,?)',[username, base64str,post],function (error, results, fields){
if (error) throw error;
return res.send({error: false,data: results, message: 'Post Created'});
});
});

app.listen(port,function(){
console.log('App running on Port: '+port);
});
module.exports = app;

我立即调用/api/createpost,我开始得到这个CORS错误,所有模块立即停止工作。

现在我得到错误503从各种可能使用后端服务器。请问我需要建议,有什么我做得不对吗?请建议。

编辑

现在查看我的日志,我看到这个错误

^

2021-10-28T06:21:46.942991+00:00 app[web.1]: 
2021-10-28T06:21:46.942992+00:00 app[web.1]: Error: ER_BAD_NULL_ERROR: Column 'username' cannot be null
2021-10-28T06:21:46.942993+00:00 app[web.1]:     at Query.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
2021-10-28T06:21:46.943002+00:00 app[web.1]:     at Query.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
2021-10-28T06:21:46.943002+00:00 app[web.1]:     at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
2021-10-28T06:21:46.943002+00:00 app[web.1]:     at Parser._parsePacket (/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
2021-10-28T06:21:46.943003+00:00 app[web.1]:     at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
2021-10-28T06:21:46.943003+00:00 app[web.1]:     at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
2021-10-28T06:21:46.943003+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
2021-10-28T06:21:46.943004+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
2021-10-28T06:21:46.943004+00:00 app[web.1]:     at Socket.emit (events.js:400:28)
2021-10-28T06:21:46.943004+00:00 app[web.1]:     at addChunk (internal/streams/readable.js:293:12)
2021-10-28T06:21:46.943005+00:00 app[web.1]:     --------------------
2021-10-28T06:21:46.943005+00:00 app[web.1]:     at Pool.query (/app/node_modules/mysql/lib/Pool.js:199:23)
2021-10-28T06:21:46.943005+00:00 app[web.1]:     at /app/iassclone.js:86:12
2021-10-28T06:21:46.943006+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-10-28T06:21:46.943006+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:137:13)
2021-10-28T06:21:46.943006+00:00 app[web.1]:     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2021-10-28T06:21:46.943006+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-10-28T06:21:46.943007+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:281:22
2021-10-28T06:21:46.943007+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2021-10-28T06:21:46.943007+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-10-28T06:21:46.943007+00:00 app[web.1]:     at urlencodedParser (/app/node_modules/body-parser/lib/types/urlencoded.js:100:7) {
2021-10-28T06:21:46.943007+00:00 app[web.1]:   code: 'ER_BAD_NULL_ERROR',
2021-10-28T06:21:46.943008+00:00 app[web.1]:   errno: 1048,
2021-10-28T06:21:46.943008+00:00 app[web.1]:   sqlMessage: "Column 'username' cannot be null",
2021-10-28T06:21:46.943008+00:00 app[web.1]:   sqlState: '23000',
2021-10-28T06:21:46.943008+00:00 app[web.1]:   index: 0,
2021-10-28T06:21:46.943008+00:00 app[web.1]:   sql: 'INSERT INTO icloneuserspost (username, base64str, post) VALUES (NULL,NULL,NULL)'
2021-10-28T06:21:46.943009+00:00 app[web.1]: }
2021-10-28T06:21:46.951291+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-28T06:21:46.951459+00:00 app[web.1]: npm ERR! errno 1
2021-10-28T06:21:46.954188+00:00 app[web.1]: npm ERR! iassgb@1.0.0 start: `node iassclone.js`
2021-10-28T06:21:46.954234+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-28T06:21:46.954289+00:00 app[web.1]: npm ERR! 
2021-10-28T06:21:46.954327+00:00 app[web.1]: npm ERR! Failed at the iassgb@1.0.0 start script.
2021-10-28T06:21:46.954364+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-28T06:21:46.957295+00:00 app[web.1]: 
2021-10-28T06:21:46.957381+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-28T06:21:46.957416+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-10-28T06_21_46_954Z-debug.log
2021-10-28T06:21:47.069221+00:00 heroku[web.1]: Process exited with status 1
2021-10-28T06:21:47.145896+00:00 heroku[web.1]: State changed from up to crashed
2021-10-28T06:21:47.188605+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-28T06:21:49.154572+00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-28T06:21:50.325243+00:00 heroku[web.1]: State changed from starting to up
2021-10-28T06:21:50.051231+00:00 app[web.1]: 
2021-10-28T06:21:50.051244+00:00 app[web.1]: > iassgb@1.0.0 start /app
2021-10-28T06:21:50.051244+00:00 app[web.1]: > node iassclone.js
2021-10-28T06:21:50.051245+00:00 app[web.1]: 
2021-10-28T06:21:50.197769+00:00 app[web.1]: App running on Port: 44770

它说列用户名不能为NULL,我利用用户名从localstorage.getItem()保存在浏览器中,但它仍然告诉我它是NULL。在DB中保存帖子的post请求如下所示:

function createNewPost(){
let username = localStorage.getItem('userinfo');
let base64str = localStorage.getItem('postImage');
let item = {username, post, base64str};
fetch('https://m2d3srv.herokuapp.com/api/createpost',{
method: 'POST',
mode: 'cors',
headers:{
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: JSON.stringify(item)
}).then((response) => response.json())
.then((responseJson)=>{
if(responseJson.message ==='Post Created'){
alert("Post Created");
}else{
alert(responseJson.message);
}
}).catch((error)=>{
console.error(error);
})
}

我修好了

'Content-Type': 'multipart/form-data',

这就是挑战,改成'Content-Type': 'application/json',

固定它。它现在将base64作为BLOB

发布

相关内容

  • 没有找到相关文章

最新更新