我最近看到了一个从这里上传一些照片的教程[https://aguacatelang.wordpress.com/2012/08/19/android-multipart-upload-to-node-js/][1]
我刚刚学习了nodejs并且对编程语言的结构了解不足,我发现了这样的错误:
home/je/Documents/BE/UploadFoto/app.js:12
var db = new Db('photos', new dbServer('localhost', dbConnection.'27017', {}))
^^^^^^^
SyntaxError: Unexpected string
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
这是原始源代码:
var express = require('express');
var app = express()
var fs = require('fs');
var im = require('imagemagick');
var Db = require('mongodb').Db;
var dbServer = require('mongodb').Server;
var dbConnection = require('mongodb').Connection;
var db = new Db('photos', new dbServer('localhost', dbConnection.'DEFAULT_PORT', {}));
db.open(function(err, db){});
app.use(express.bodyParser())
app.get('/', function(req, res){
res.send(
'<form action="/upload" method="post" enctype="multipart/form-data">'+
'<input type="file" name="source">'+
'<input type="submit" value="Upload">'+
'</form>'
);
});
app.post('/upload', function(req, res){
console.log("Received file:\n" + JSON.stringify(req.files));
var photoDir = __dirname+"/photos/";
var thumbnailsDir = __dirname+"/photos/thumbnails/";
var photoName = req.files.source.name;
fs.rename(
req.files.source.path,
photoDir+photoName,
function(err){
if(err != null){
console.log(err)
res.send({error:"Server Writting No Good"});
} else {
im.resize(
{
srcData:fs.readFileSync(photoDir+photoName, 'binary'),
width:256
},
function(err, stdout, stderr){
if(err != null){
console.log('stdout : '+stdout)
res.send({error:"Resizeing No Good"});
} else {
//console.log('ELSE stdout : '+stdout)
fs.writeFileSync(thumbnailsDir+"thumb_"+photoName, stdout, 'binary');
res.send("Ok");
}
}
);
}
}
);
});
app.get('/info', function(req, res){
console.log(__dirname);
res.send("ok");
});
app.listen(8000);
console.log('connected to localhost....')
我将DEFAULT_PORT切换为27017,因为在我的PC中使用了mongodb的端口:localhost / 27017。愿有人帮帮我吗?谢谢