Skip to content

Instantly share code, notes, and snippets.

@jsstoni
Last active December 13, 2016 01:04
Show Gist options
  • Save jsstoni/d549d411b4c4c743dd771acce45ff20e to your computer and use it in GitHub Desktop.
Save jsstoni/d549d411b4c4c743dd771acce45ff20e to your computer and use it in GitHub Desktop.
upload files
const express = require('express')
const app = module.exports = express()
const path = require('path')
const port = process.env.PORT || 8080
const fs = require('fs')
const bodyParser = require('body-parser')
const multipart = require('connect-multiparty')
app.set("view options", {layout: false})
//configurar una ruta de nuestros archivos html
app.use(express.static(path.join(__dirname, 'vista')))
app.use(bodyParser.urlencoded({
extended: false,
keepExtensions:true
}))
app.use(multipart())
app.get('/', (req, res) => {
res.render('index.html')
})
app.post('/imagen', (req, res) => {
//archivo temporal
var path = req.files.img.path
//destino del archivo
var newPath = './files/'+req.files.img.name
//comprobar si el tipo de archivo a subir es una imagen
if (req.files.img.type.indexOf('image')==-1) {
res.send("no es una imagen");
}else {
//subir el archivo temporal
fs.link(path, newPath, (err) => {
if (err) throw err;
//eliminar el archivo que se creo temporalmente
fs.unlink(path, () => {
res.send('subido con exito');
})
})
}
})
const server = app.listen(port, () => {
console.log('Server listening on port ' + port)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment