Update/remove page ESP8266 Webserver - arduino

I'm using the ESP8266WebServer.h library for the ESP8266. Files can be served to a specific filename by using something like:
...
void example() {
sendFile(200, "text/html", data_example, sizeof(data_example));
}
...
webServer.on ("/example.html", example);
Once a file is served it cannot be updated by executing server.on ("/example.html", example2);.
How can a hosted file be removed (or updated to a blank file) so that it will return a 404 error ?

why not include a conditionnal logic in your example() function in order serve a 404 page when necessary ?
Hope it helps.

Related

Vue js deploying in production seems harder then it is

I've created simple Vue JS App using Vue-cli 3. I have a digitalocean droplet with Nginx + php.
My goal is to host Vue App on the same droplet.
I have already tried:
Add certain location / to nginx conf.
Used this simple node server: https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
And what I get i sUncaught SyntaxError: Unexpected token <:
Interesting thing, when I use npm run build to make production resources into dist directory and use php -S localhost:8080 it is hosted like a charm. But the same thing with simple node js server causes the result on the screenshot above.
I've been struggling for 2 days straight. Halp me please.
I was struggling with the same issue. The problem was that when the browser requests the javascript and css files, the contents of the index file is sent instead.
Solved it by following step 3 of this tutorial.
Since Vue is only a frontend library, the easiest way to host it and
do things like serve up assets is to create a simple Express friendly
script that Heroku can use to start a mini-web server. Read up quickly
on Express if you haven’t already. After that, add express:
npm install express --save
Now add a server.js file to your project’s root directory:
// server.js
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
app.listen(port);
console.log('server started '+ port);
After that, since the idea is to serve index.html in the first place, I borrowed this idea and added the following to my code (notice I used sendFile if the request is html):
app.use((req, res, next) => {
res.status(404)
if (req.accepts("html")) {
res.sendFile(path.join(__dirname + "/dist/index.html"))
return
}
if (req.accepts("json")) {
res.send({error: "Not found"})
return
}
res.type("txt").send("Not found")
})
A little bit late, but I have used this to sendFile index.html up on 404 response.
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(404).sendFile(path.resolve(__dirname, "dist", "index.html"));
})
Explanation:
err will be defined when the request is erroneous. res.status will set the status to 404 code and the app will sendFile the resolved index.html. (Notice that I used __dirname and path.resolve together to build an absolute path to be safe.

Golang net/http fileserver giving 404 on any pattern other than "/"

Hello awesome stackoverflow community,
Apologies for the lame question.
I've been playing around with the net/http package in Go, and was trying to set an http.Handle to serve the contents of a directory. My code to the Handle is
func main() {
http.Handle("/pwd", http.FileServer(http.Dir(".")))
http.HandleFunc("/dog", dogpic)
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
My dogpic handler is using os.Open and an http.ServeContent, which is working fine.
However, when I try to browse localhost:8080/pwd I am getting a 404 page not found, but when I change the pattern to route to /, as
http.Handle("/", http.FileServer(http.Dir(".")))
it is showing the contents of the current page. Can someone please help me figure out why the fileserver is not working with other patterns but only /?
Thank you.
The http.FileServer as called with your /pwd handler will take a request for /pwdmyfile and will use the URI path to build the filename. This means that it will look for pwdmyfile in the local directory.
I suspect you only want pwd as a prefix on the URI, not in the filenames themselves.
There's an example for how to do this in the http.FileServer doc:
// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
You'll want to do something similar:
http.Handle("/pwd", http.StripPrefix("/pwd", http.FileServer(http.Dir("."))))
you should write http.Handle("/pwd", http.FileServer(http.Dir("./")))
http.Dir references a system directory.
if you want localhost/ then use http.Handle("/pwd", http.StripPrefix("/pwd", http.FileServer(http.Dir("./pwd"))))
it will serve all you have into /pwd directory at localhost/

Go server not serving files properly

I am creating a SPA.
I am trying to respond all requests with index.html
(I handle routing on the frontend).
My directory structure look like this:
Backend
-- main.go
Frontend
..(some other files)..
-- index.html
Whole project is located in "C:\Go\Projects\src\github.com\congrady\Bakalarka"
My main.go file looks like this:
package main
import (
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "../Frontend/index.html")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
When I run my main.go file (using go run), my localhost always responds with "404 page not found".
When I try to serve static content using fmt, everything works fine.
Please help, I'm stuck on this for a really long time and I can't get it to work.
Thanks
Be aware that if you hardcode relative paths in your source file, the directory which you are in when starting the app matters.
In the current configuration, make sure to start the app from the Backend directory, i.e.
C:\Go\Projects\src\github.com\congrady\Bakalarka\Backend,
NOT your apps root directory
C:\Go\Projects\src\github.com\congrady\Bakalarka
or
change the string in the main file to Frontend/index.html and run from
C:\Go\Projects\src\github.com\congrady\Bakalarka

In nginx config, how to show something to log file directly in the config file?

I'm config a nginx, and debugging the config file,
How to show something from the config file directly to log file?
for example:
location ..... {
to_log "some string";
}
There is no direct way (on the todo list of the echo nginx module), but this solution seems fine https://serverfault.com/questions/404626/how-to-output-variable-in-nginx-log-for-debugging

nodejs read image headers

I'm writing a script to download files from urls in a list. The problem I'm having is that the urls don't just point to static files, like file.jpg, they tend to point to servlets that return a file.
What I want to do is download the file for each url and save it with a generic name, then read its headers and rename it with the appropriate extension. (Unless there's a better way)
How could I do that?
I've tried using mime-magic, but it tells me that the extension-less files are directories.
It should work using mime-magic. Are you sure the path is correct and the path is not pointing to a directory?
Otherwise you could use the command line tool file --mime /path/to/file
Here is how to detect an extension of a file using mime-magic:
mime('/path/to/foo.pdf', function (err, type) {
if (err) {
console.error(err.message);
// ERROR: cannot open `/path/to/foo.pdf' (No such file or directory)
} else {
console.log('Detected mime type: %s', type);
// application/pdf
}
});
Note: Added sled's comment as an answer under community-wiki.

Resources