Created
May 28, 2017 01:26
-
-
Save evdokimovm/75d2754f3b4697daf6bfad543d3b7f35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h1>Hello</h1> | |
<img src="<%= data[0].path %>" alt=""> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>File Uploading Form</title> | |
</head> | |
<body> | |
<h3>File Upload:</h3> Select a file to upload: | |
<br /> | |
<form action="/file_upload" method="POST" enctype="multipart/form-data"> | |
<input type="file" name="file" size="50" /> | |
<br /> | |
<input type="submit" value="Upload File" /> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example", | |
"main": "server.js", | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"ejs": "^2.5.6", | |
"ejs-mate": "^2.3.0", | |
"express": "^4.15.3", | |
"mongoose": "^4.10.3", | |
"multer": "^1.3.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h1>Hello</h1> | |
<a href="/another">Another</a> | |
<br> | |
<img src="<%= data[0].path %>" alt=""> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
var app = express() | |
var ejs = require('ejs') | |
var engine = require('ejs-mate') | |
var multer = require('multer') | |
var fs = require('fs') | |
var path = require('path') | |
var bodyParser = require('body-parser') | |
var mongoose = require('mongoose') | |
mongoose.connect('mongodb://localhost:27017/testdatabase') | |
var Schema = mongoose.Schema | |
var ImagesModel = new Schema({ | |
path: String | |
}, { | |
collection: 'images' | |
}) | |
var Model = mongoose.model('Model', ImagesModel) | |
app.get('/sample', function(req, res, next) { | |
Model | |
.find({}) | |
.lean() | |
.exec(function(err, data) { | |
if (err) return next(err) | |
res.render('sample', { | |
data: data | |
}) | |
}) | |
}) | |
app.get('/another', function(req, res, next) { | |
Model | |
.find({}) | |
.lean() | |
.exec(function(err, data) { | |
if (err) return next(err) | |
res.render('another/another', { | |
data: data | |
}) | |
}) | |
}) | |
app.use(express.static('uploads')) | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.engine('ejs', engine) | |
app.set('view engine', 'ejs') | |
app.get('/', function(req, res) { | |
res.sendFile(__dirname + "/" + "index.html") | |
}) | |
var storage = multer.diskStorage({ | |
destination: function(req, file, callback) { | |
callback(null, './uploads') | |
}, | |
filename: function(req, file, callback) { | |
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)) | |
} | |
}) | |
var upload = multer({ storage: storage }) | |
app.post('/file_upload', upload.single('file'), function(req, res) { | |
var image = new Model() | |
image.path = req.file.filename | |
image.save() | |
res.redirect('/sample') | |
}) | |
app.listen(8080, function() { | |
console.log('Node.js listening on port ' + 8080) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── views/ | |
| ├── sample.ejs | |
| └── another/ | |
| └── another.ejs | |
├── uploads/ | |
├── package.json | |
├── server.js | |
└── index.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment