我正在使用node.js创建一个小博客,我的博客在本地完美运行,但我想将图像添加到我的文章中,我已经使用bootstrap来显示我的输入,但在 Node 上。我无法保存我的图像,你能帮我吗?
我使用 Mongoose 作为在线服务器 (我在linux下工作)
服务器.js
var express = require('express'),
swig = require('swig'),
path = require('path'),
mongoose = require('mongoose'),
app = express();
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname+'/views');
mongoose.connect('mongodb://**:**@**.mlab.com:**/blog');
var Article = mongoose.model('Article', {title: String, content:
String, updated: Date});
app.get('/', function(req, res){
Article.find({}).sort({updated: -1}).exec(function(err, articles){
if(err){throw err;}
data = {title: 'Mon super blog', articles: articles};
res.render('index', data);
})
});
app.get('/article/:id', function(req, res){
var article = Article.findById(req.params.id, function(err,
article){
if(err){throw err}
var data = {article: article, title: article.title};
res.render('article', data);
});
});
app.post('/update/:id', function(req, res){
Article.update({ _id : req.params.id}, {
title: req.body.titre, content: req.body.contenu, updated: new
Date()
},
function(err){
if(err){throw err;}
res.redirect('/');
});
});
app.get('/edit/:id', function(req, res){
var article = Article.findById(req.params.id, function(err,
article){
if(err){throw err}
var data = {article: article, title: 'Modifier '
+article.title};
res.render('edit', data);
})
})
app.get('/destroy/:id', function(req, res){
Article.remove({ _id : req.params.id}, function(err){
if(err){throw err;}
res.redirect('/');
});
});
app.get('/create', function(req, res){
var data = {title: 'Ajouter un article'};
res.render('create', data);
});
app.post('/store', function(req, res){
var article = new Article({
title: req.body.titre,
content: req.body.contenu,
updated: new Date()
});
article.save(function(err, article){
if(err){throw err;}
res.render('created');
});
});
app.listen(3000);
console.log('App running http://localhost:3000');
创建.html
{% extends 'layout.html' %}
{% block content %}
<form role="form" action="/store" method="post">
<div class="form-group">
<label for="titre">Titre</label>
<input type="text" name="titre" class="form-control"
placeholder="titre de l'article" required>
</div>
<div class="form-group">
<label for="titre">Titre</label>
<label for="contenu">Contenu de l'article</label>
<textarea name="contenu" class="form-control" placeholder="Contenu
de l'article" rows="3" required></textarea>
</div>
<form>
<div class="form-group">
<label>Telecharger une image</label>
<input type="file" class="form-control-file" name="img">
</div>
</form>
<button type="submit" class="btn btn-default">Ajouter </button>
</form>
{% endblock %}
文章.html
{% extends 'layout.html' %}
{% block content %}
<span class="pull-right"><a class="btn btn-danger remove"
href="/destroy/{{article.id}}">Le bouton magique</a></span>
<h1>{{article.title}}</h1>
<p>{{article.content}}</p>
<span class="date">
Mis à jour {{article.updated.getDate()}} /
{{article.updated.getMonth() +1}} / {{article.updated.getFullYear()}}
</span>
<div class="pull-right">
<a href="/edit/{{article.id}}" class="btn btn-default">Modifier</a>
</div>
{% endblock %}
最佳答案
您可以使用multer库用于上传文件并将其保存到本地存储中,并将相对路径保存到数据库。
var multer = require('multer');
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads')
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({storage: storage});
app.post('/create',upload.single('img'), function(req,res)
{
var article = new Article({
title: req.body.titre,
content: req.body.contenu,
updated: new Date(),
// you can store path here
});
article.save(function(err, article){
if(err){throw err;}
res.render('created');
});
});
您可以在 here 阅读更多内容
但我建议使用任何云存储(例如 s3 或 Azure 存储)并直接从客户端存储文件,并将该 url 传递到服务器以存储在数据库中。
Client > Requst server for temporary cred > upload file using that cred > call
/create
on server including url.
关于javascript - 我想使用 node.js 将图像上传到我的博客,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48986925/