res.sendfile("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgpublic/index1.html"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg); climate I obtain a server console warning

express deprecated res.sendfile: use res.sendFile instead

but it works fine top top the client side.

You are watching: Typeerror: path must be absolute or specify root to res.sendfile

But as soon as I change it to

res.sendFile("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgpublic/index1.html"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);I gain an error

TypeError: path should be pure or specify source to res.sendFile

and index1.html is not rendered.

I am unable to number out what the absolute path is. I have public brochure at the same level as server.js. I am law the res.sendFile from with server.js. Ns have additionally declared app.use(express.static(path.join(__dirname, "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgpublic"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg)));

Adding my magazine structure:

/Users/sj/test/....app/........models/....public/........index1.htmlWhat is the absolute course to it is in specified here ?

I"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgm utilizing Express 4.x.


Answers


88
The express.static middleware is separate from res.sendFile, therefore initializing it through an absolute course to your public catalog won"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgt execute anything come res.sendFile. You have to use an pure path directly with res.sendFile. There are two straightforward ways to execute it:

res.sendFile(path.join(__dirname, "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg../public"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgindex1.html"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg));res.sendFile("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgindex1.html"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, root: path.join(__dirname, "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg../public"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg) );

Note: __dirname return the directory that the currently executing manuscript is in. In your case, that looks prefer server.js is in app/. So, to acquire to public, you"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgll need ago out one level first: ../public/index1.html.

Note: path is a integrated module that needs to be forced for the above code come work: var path = require("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgpath"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);


Saturday, June 5, 2021
*

answer 7 Months ago
ojrac
18

you need to use a config document that will certainly be contained in each record first line, for example your application look favor this

root / app / Plugins

inside your root dir : app-config.php

if ( !defined("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgABSPATH"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg) ) define("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgABSPATH"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, dirname(__FILE__) . "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg/"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);now, intend you have to incorporate a plugin file, so

inside your Plugin dir : my-plugin.php

require_once "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg../../app-config.php"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg;now everything listed below this line have the right to use ABSPATH

example execute you desire to pack an image

*
now, the thing is much more simple if your app is designed to automatically load some records like

plugin-widget-1.phpso that every little thing inside this file or any kind of other record loaded by the my-plugin.php paper can use the ABSPATH without encompass each time the app-config.php file.

See more: Sniffling You Are All So Ugly Meme, #You Are All So Ugly On Tumblr

with this in mind you deserve to have every the short-hand friend want right into the app-config.php example

define("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgUPLOAD_PATH"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, ABSPATH. "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgPublic/uploads/"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);define("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgIMAGES_PATH"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, ABSPATH. "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgPublic/images/"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);define("https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgHELPERS_PATH"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg, ABSPATH. "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgApp/helpers/"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg);...so, now that you have actually all defined, if you require to move a file, let"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpgs say one folder forward example:

root / app / Plugins / Utils

just inlucde require_once "https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg../../../app-config.php"https://discoverhotmail.com/typeerror-path-must-be-absolute-or-specify-root-to-res-sendfile/imager_2_3925_700.jpg;

obviously i expect that you are not transforming paths each time =) quiet if you have to do so is always much more simple to adjust one record inclusion instead of hundreds.