...
index.html
<body>
<h1>upload test</h1>
<!--<form method="post" enctype="multipart/form-data" action="/picture"> -->
<form id="pictureForm" method="post" enctype="multipart/form-data">
<div>
<label for="file">Choose file to upload</label>
<input type="file" name="file" id="pictureFile" multiple>
</div>
<div>
<button>Submit</button>
</div>
</form>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="./upload.js"></script>
</body>
...
upload.js
...
await axios({
method: 'post',
url: '/upload',
data: formData,
headers: {
'Content-Type': 'multipart/form-data',
},
})
...
server.js
...
app.get('/', async (req, res, next) => {
try {
res.writeHead(200, { 'Content-Type': 'text/html' });
const data = await fs.readFile('../index.html')
res.write('Hello nodejs');
res.end(data);
next();
}
catch (err) {
console.error(err);
}
});
app.post('/upload', upload.single('file'), async (req, res, next) => {
try {
res.send('work'); <-- not working
}
});
...
i'm making image upload server. and i success. i can upload image into particular directory that i want.
problem is, i want to see the phrase 'success' using res.end()(or res.send, etc). but web page was not change!
correctly, web got the response. but i don't know how to show this response on page.
please teach me.
On the frontend you can do something like this
fetch("http://localhost:5500/upload", requestOptions).then(response => response.text())