Created
July 10, 2019 21:35
-
-
Save Kijacode/2acddb40bbfb53f77d1859c1c76bb06d to your computer and use it in GitHub Desktop.
This Gist help You to upload file in Nodejs by using Expressjs in backend , and through Reactjs your are able to interact with backend,
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
.App { | |
text-align: center; | |
} | |
.App-logo { | |
animation: App-logo-spin infinite 20s linear; | |
height: 40vmin; | |
pointer-events: none; | |
} | |
.App-header { | |
background-color: #282c34; | |
min-height: 100vh; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
font-size: calc(10px + 2vmin); | |
color: white; | |
} | |
.App-link { | |
color: #61dafb; | |
} | |
@keyframes App-logo-spin { | |
from { | |
transform: rotate(0deg); | |
} | |
to { | |
transform: rotate(360deg); | |
} | |
} |
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
import React, { Component } from "react"; | |
import "./App.css"; | |
import axios from "axios"; | |
class App extends Component { | |
constructor() { | |
super(); | |
this.onChangeHandler = this.onChangeHandler.bind(this); | |
} | |
state = { | |
selectedFile: null | |
}; | |
onChangeHandler = event => { | |
this.setState({ | |
selectedFile: event.target.files[0] | |
}); | |
}; | |
onClickHandler = () => { | |
const data = new FormData(); | |
data.append("file", this.state.selectedFile); | |
// fetch("api", { | |
// method: "POST", | |
// headers: { "Content-Type": "application/json" }, | |
// body: JSON.stringify(data) | |
// }) | |
// .then(response => { | |
// alert(response); | |
// }) | |
// .catch(error => { | |
// console.log(error); | |
// }); | |
axios.post("http://localhost:8000/api/Upload", data) | |
.then(res => { | |
// res.statusText(); | |
alert(res.data); | |
}); | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<form> | |
<div> | |
<label> | |
<input | |
type="file" | |
placeholder="file" | |
onChange={this.onChangeHandler} | |
/>{" "} | |
</label>{" "} | |
</div>{" "} | |
<button onClick={this.onClickHandler} type="button"> | |
Upload{" "} | |
</button>{" "} | |
</form>{" "} | |
</div> | |
); | |
} | |
} | |
export default App; |
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
const express = require("express"); | |
const userRouter = require("../router/router"); | |
const app = express(); | |
app.use(userRouter); | |
module.exports = app; |
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
body { | |
margin: 0; | |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | |
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | |
sans-serif; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
} | |
code { | |
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | |
monospace; | |
} |
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" /> | |
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="theme-color" content="#000000" /> | |
<!-- | |
manifest.json provides metadata used when your web app is installed on a | |
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | |
--> | |
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | |
<!-- | |
Notice the use of %PUBLIC_URL% in the tags above. | |
It will be replaced with the URL of the `public` folder during the build. | |
Only files inside the `public` folder can be referenced from the HTML. | |
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | |
work correctly both with client-side routing and a non-root public URL. | |
Learn how to configure a non-root public URL by running `npm run build`. | |
--> | |
<title>React App</title> | |
</head> | |
<body> | |
<noscript>You need to enable JavaScript to run this app.</noscript> | |
<div id="root"></div> | |
<!-- | |
This HTML file is a template. | |
If you open it directly in the browser, you will see an empty page. | |
You can add webfonts, meta tags, or analytics to this file. | |
The build step will place the bundled scripts into the <body> tag. | |
To begin the development, run `npm start` or `yarn start`. | |
To create a production bundle, use `npm run build` or `yarn build`. | |
--> | |
</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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import './index.css'; | |
import App from './App'; | |
import * as serviceWorker from './serviceWorker'; | |
ReactDOM.render(<App />, document.getElementById('root')); | |
// If you want your app to work offline and load faster, you can change | |
// unregister() to register() below. Note this comes with some pitfalls. | |
// Learn more about service workers: https://bit.ly/CRA-PWA | |
serviceWorker.unregister(); |
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
const router = require("express").Router(); | |
const bodyParser = require("body-parser"); | |
const multer = require("multer"); | |
const cors = require("cors"); | |
router.use(cors()); | |
router.use(bodyParser.urlencoded({ extended: true })); | |
var storage = multer.diskStorage({ | |
destination: function(req, file, cb) { | |
cb(null, "E:\\desktop\\Andela\\ALCwithforloopTz\\fileupload\\folders"); | |
}, | |
filename: function(req, file, cb) { | |
cb( | |
null, | |
Date.now() + | |
"-" + | |
file.fieldname + | |
file.size + | |
file.path + | |
file.originalfilename + | |
file.buffer | |
); | |
} | |
}); | |
var upload = multer({ storage: storage }).single("file"); | |
router.post("/api/Upload", function(req, res) { | |
upload(req, res, function(err) { | |
if (err) { | |
return res.end("Something went wrong!"); | |
} | |
// return res.status(200).send(req.file); | |
console.log(req.file); | |
}); | |
// upload(req, res, function(err) { | |
// if (err instanceof multer.MulterError) { | |
// return res.status(500).json(err); | |
// } else if (err) { | |
// return res.status(500).json(err); | |
// } | |
// return res.status(200).send(req.file); | |
// }); | |
}); | |
module.exports = router; |
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
const http = require("http"); | |
const app = require("../router/app"); | |
app.set("port", process.env.PORT || 8000); | |
const server = http.createServer(app, function() { | |
console.log("server starting"); | |
}); | |
server.listen(process.env.PORT || 8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment