Comments (4)
You have a couple of options:
src/test/resources
is mapped into the classpath by Maven when you run tests. You can get an input stream by getting a reference to your nanohttpd server, and callinggetResourceAsStream()
@Override public Response serve(HTTPSession session) {
String uri = session.getUri();
InputStream stream = getClass().getResourceAsStream(uri);
if (stream == null) {
return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found: "+uri);
}
return new Response(Response.Status.OK, MIME_HTML, stream);
}
- Alternatively the starting base directory when running tests is usually the project itself. That means you could prepend "src/test/resources" onto the URI and go looking for a file at that location.
from nanohttpd.
@psh Thanks!
One more thing, not all the files will be HTML. Can this be generalized to arbitrary mimetypes?
from nanohttpd.
The constant being passed for the mime type is just a String, so feel free to use whatever suits the file you have. Check out the "webserver" sub-project for code that associates file extensions to their appropriate mime type if you need an idea of what to do.
from nanohttpd.
@psh Thanks again!
Could you tweak the example to make use of webserver
associations?
from nanohttpd.
Related Issues (20)
- Using the webserver disables "caches" object/API on the browser?
- permessage-deflate for WebSockets HOT 2
- Can't set response status HOT 1
- postData still contains the chunksize HOT 1
- nanohttpd Android: how to run a complex website on local server? HOT 2
- [SECURITY] Vulnerability in nanohttpd
- an infinite loop bug
- collides with a type
- Broken pipe HOT 2
- When can I fix a known problem
- addHeader doesn't have additive behavior
- multiple request headers with same name aren't preserved
- server is overly opinionated about what is a valid HTTP verb
- How to stream a page instead of building entire strings and delivering them
- Could not find com.github.edrlab.nanohttpd:nanohttpd:master-SNAPSHOT HOT 1
- BAD REQUEST: Missing URI. Usage: GET /example/file.html HOT 1
- 使用它有没有什么限制? HOT 2
- how can access from out of app?
- Use it as a WebServer, but close the client after client connect
- Unable to use the latest version in Android
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nanohttpd.