admin管理员组文章数量:1293399
I'm trying to build my first webapp, I started with the frontend and using jquery and jquery mobile as well as many plugins I have a significant frontend already, and all of it stems from a single html file (since jquery mobile uses page divs within the same file) but there is also a main js file for the app, a css file and many css and js files included from plugins and the like. I'm now trying to add in database and other backend functionality using node.js and express.js, but I've run into a wall, when I use res.sendFile() to serve up the html the scripts and css don't load, and when I try to serve the directory everything is in it shows the directory as links which I certainly don't want in the public view (though when I do this and click the html file link it works fine.
What I want to know is how do I use express to a) serve up an externally designed and maintained frontend and b) allow that frontend to send requests back to the server (so I can use forms and get data and stuff)?
I'm trying to build my first webapp, I started with the frontend and using jquery and jquery mobile as well as many plugins I have a significant frontend already, and all of it stems from a single html file (since jquery mobile uses page divs within the same file) but there is also a main js file for the app, a css file and many css and js files included from plugins and the like. I'm now trying to add in database and other backend functionality using node.js and express.js, but I've run into a wall, when I use res.sendFile() to serve up the html the scripts and css don't load, and when I try to serve the directory everything is in it shows the directory as links which I certainly don't want in the public view (though when I do this and click the html file link it works fine.
What I want to know is how do I use express to a) serve up an externally designed and maintained frontend and b) allow that frontend to send requests back to the server (so I can use forms and get data and stuff)?
Share Improve this question edited Jul 19, 2015 at 19:16 Roscode asked Jul 19, 2015 at 18:58 RoscodeRoscode 1231 gold badge1 silver badge6 bronze badges1 Answer
Reset to default 25You should do the following things:
- Serve your static files
- Create an API server that will listen for the requests coming from your frontend app
1. Serve your static files
To serve static files with Express, read this link.
You'll basically add it to your express app:
app.use( express.static( __dirname + '/client' ));
Where '/client'
will be the name of the folder with your frontend app files.
2. Create an API server
You can see how to create an API server here.
For the entry point of your application, you should send/render a file.
This could be accomplished with the following code:
app
.get( '/', function( req, res ) {
res.sendFile( path.join( __dirname, 'client', 'index.html' ));
});
This will send a static file every time that a user request a file at the root path of your application.
You can use the asterisk *
(wildcard) instead of /
too. That symbol meaning that for whatever route requested, you will respond with the same file/action.
More about the responses here.
Sum up
Those are the things that you should seek to build your app.
You can see a simple app with those things implemented here.
本文标签:
版权声明:本文标题:javascript - Using express.js to serve html file along with scripts, css, and images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739075922a2136120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论