I want to implement universal links in my project and I need to serve a json via Koa, which is a static file named apple-app-site-association.
My file is located in /assets/apple-app-site-association/apple-app-site-association folder.
My concern is that I cannot access this https://myprojectlink/apple-app-site-association.
What I have at this moment:
const path = require("path");
const Koa = require("koa");
const mount = require("koa-mount");
const serve = require("koa-better-serve");
app.use(mount("/apple-app-site-association", serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/apple-app-site-association"))));
I get Not Found, it seems like I cannot serve it in the right way.
What can I do?
Thank you very much in advance.
The koa-static module is what you are looking for. You can use this to serve a single file or entire directory to a given path. Here are a couple of examples that should help:
Serving Files With koa-static
To serve files just pass the koa-static middleware to the koa's middleware stack with app.use().
Serve an Entire Directory
Here we are serving the entire /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static')))
app.listen(3000)
Serve a Single File
Here we are serving a single file, for example, the data.json file inside of the /static directory
const Koa = require('koa')
const serve = require('koa-static')
const path = require('path')
const app = new Koa()
app.use(serve(path.join(__dirname, '/static/data.json')))
app.listen(3000)
Serve Directory or File on a Given Path
Use koa-mount to mount koa-static to a given path. For example, here we mount the entire /static directory to be served on the /public path
const Koa = require('koa')
const serve = require('koa-static')
const mount = require('koa-mount')
const path = require('path')
const app = new Koa()
app.use(mount('/public ',serve(path.join(__dirname, '/static'))))
app.listen(3000)
serve (koa-better-serve), like most static server middlewares for Node frameworks, takes a path to a directory, not a path to a single file. You can also get rid of the mount() call, koa-mount is for mounting other Koa apps as middleware.
app.use(serve(path.resolve(__dirname,"../../../assets/apple-app-site-association/")));
The official method for static-file serving is https://www.npmjs.com/package/koa-static, you can see the documentation there.
Related
I am trying to server static files with express and them dont work, i think its express problem or something but i dont realize why it dont works.
my folders look like this:
app.js
public
--css
--js
--images
and the code i trying to run like this:
app.use(express.static(path.join(__dirname,'public')));
if i do console.log of the path
console.log(path.join(__dirname, 'public'))
i get the next path
C:\Users\J\Desktop\node.js\social-media-cars\public
and if i look at the path with the windows file explorer i see that the path is correct and i see the content inside, so i dont think its path fault
i also tryed :
app.use(express.static(path.join(__dirname,'public')));
app.use('/static',express.static(path.join(__dirname, '/public')));
and dont works, always the same things:
im desesparate with this, idk why this dont works.
code where i use it looks like this:
// Import the express module
const path = require('path');
const express = require("express");
// Instantiate an Express application
const app = express();
const dotenv = require("dotenv").config();
const ActionsRouter = require('./routers/actions-router');
const UserRouter = require('./routers/user-router');
const cookieparser = require('cookie-parser');
// app.use('/static',express.static(path.join(__dirname, '/public')));
//set static files that are rendered after
app.use(express.static(path.join(__dirname,'public')));
//read the cookies from response
app.use(cookieparser(path.join(__dirname, '/public')));
console.log(path.join(__dirname, 'public'))
//create the req.body
app.use(express.json());
//user
app.use("/api",UserRouter);
app.use("/useractions",ActionsRouter);
module.exports = app;
const server = app.listen(port,()=>{
console.log(`listening on port ${port}`)});
get the static files with every response i get
also i get this error
1. Add this middleware in the index.js or app.js above you set view engine
app.use(express.static(path.join(__dirname, "public")));
2. Folder Structure
|__public/
|__ css/
|__ css files...
|__ js/
|__ js files...
3. Import this way
Now you set the path to the public directory you have to give the path public folder when you import
<link rel="stylesheet" href="/css/main.css" />
In the same way, you can import your JS files
You should now be all set up to access CSS or js as well as any file in the public directory
okey i found it, i was so lost because when i try to see the files that i got in each response on the browser, i was not getting any file,
but only when i linked css and html it downloaded the static file, idk why this works like this,
i supposed every of /public file was atached to the response you use it or not
<link rel="stylesheet" type="text/css" href="/css/styles.css">
anyway if there is a way to get the file you request or not i would like to know
I don't get it, why we don't use relative path in our app instead of this path that we get after path.join. is it not possible to use relative path like express.static('../public')?
const express = require('express');
const path = require('path');
const app = express();
const publicDir = path.join(__dirname,'../public')
app.use(express.static(publicDir));`
If you use a relative path with express.static(), then it's relative to where you launched your app, not necessarily relative to __dirname which means that merely changing the directory from where you launch your app from could break your app.
If you build a full path using __dirname, then this aspect of your app won't break if you happen to launch your app from a different directory. This is just more robust. If you intend for the path to be relative to __dirname, then you may as well enforce that in your code.
I'm fairly new to CRA, and normally I just use regular React with Webpack. I'm trying to serve my CRA just like I would any other React apps. My root directory file structure currently looks something like this:
controllers/
build/
app.js
package.json
Where "app.js" is my Node/Express server, build/ is the build folder for my CRA application (the index.hmtl, static directory, manifest, etc. are all here), and controllers/ is my router files. The router file I have to serve my react, called "staticController.js", is in the controllers/ directory and looks like this:
const express = require("express");
const path = require("path");
const router = express.Router();
router.get("/client/static/home", (req, res) => {
res.sendFile(path.join(__dirname, ".." , "/build/index.html"));
})
module.exports = router;
I import this router module into app.js to use it, and run the router on port 3001. When I go to localhost:3001/client/static/home, I get a blank page and an error saying:
GET http://localhost:3001/static/css/1.1ee5c864.chunk.css
net::ERR_ABORTED 404 (Not Found)
I'm not sure how to specifiy to CRA that the static directory and all associated build files should be under the "build/" directory. So instead, I move the static folder outside the build directory, into the root directory. The file structure looks like this now:
controllers/
build/
static/
app.js
package.json
When I try the route again, I still get the same error. Now I'm a bit confused as to how Node file structure works. If app.js is in the root directory, wouldn't localhost:3001/static/ just be my static/ folder? Why can't the CRA app find this folder?
use
express.static
to serve the build folder (or any static files)
e.g. : here i'm serving the build folder on the root path (assuming it's located on the same directory as the server file)
app.use('/', express.static('build'));
I believe what you are looking for is static.
https://expressjs.com/en/4x/api.html#express.static
You need something like
var express = require('express');
var app = express();
app.use(express.static('static'));
I am using express-http-proxy
basic setup
const express = require('express');
const proxy = require('express-http-proxy');
app.use('/api/', proxy(`http://api-url/`));
app.listen(3000);
works as expected, except static files. they are point to proxy host instead of proxy route
http://localhost:3000/bundle.js
it should be
http://localhost:3000/api/bundle.js
I am assuming that bundle.js is a file hosted on the proxy host. Wherever it is referenced you may have to use relative paths so that it does not go to the root of the url.
I have a directory with HTML files which I serve with express:
var express = require('express');
var app = express();
var server = require('http').Server(app);
server.listen(8080);
// Serve /web subdirectory of this directory
app.use(express.static(__dirname+"/web"));
This allows anyone to access a file in web/ directory to access it via http://X.X.X.X:8080/file.html.
But I also have another directory somewhere else. Basically I would like to serve ../../some_directory as http://X.X.X.X:8080/some_directory/ including any of it's subdirectories.
How can I do it? Is there something like app.use_as("file path", "URL path") to serve path as URL?
You can pass the path to use as the first argument of app.use (reference):
app.use("/some_directory/", express.static(__dirname + "/../../some_directory/"));
This will serve files ../../some_directory/file directory under http://host/some_directory/file, including subdirectories.