javascript - Using express.js to serve html file along with scripts, css, and images - Stack Overflow

I'm trying to build my first webapp, I started with the frontendand using jquery and jquery mobi

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 badges
Add a comment  | 

1 Answer 1

Reset to default 25

You should do the following things:

  1. Serve your static files
  2. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1739075922a4100283.html

相关推荐

  • 聊聊Spring AI Alibaba的BilibiliDocumentReader

    序本文主要研究一下Spring AI Alibaba的BilibiliDocumentReaderBilibiliDocumentReadercommunitydocument-readersspring-ai-alibaba-star

    1小时前
    00
  • 监控ups的工具有没有?有!~老厉害了!~

    大家好,波哥又来给大家推荐好东西啦!欢迎大家在评论区留言评论自己想了解的工具、方向或职业等互联网相关内容,点赞和推荐多的,波哥会优先安排解答!关注波哥简介在数字化基础设施的关键领域,不间断电力保障系统(UPS)的智能管控已成为现代运

    1小时前
    00
  • TapData × 梦加速计划

    点击上方蓝字 关注我们 SUBSCRIBE to US在实时跃动的数据节拍中,TapData 与 AI 共舞,踏出智能未来的新一步。4月10日,由前海产业发展集团、深圳市前海梦工场、斑马星球科创加速平台等联合发起的「梦加速计划·下一位独角兽

    1小时前
    00
  • 芭比风AI玩偶席卷全网:ChatGPT几分钟打造你的时尚分身!

    新智元报道编辑:英智【新智元导读】一股由ChatGPT引爆的AI玩偶热潮正在席卷全球社交媒体!从领英到TikTok,人人都在将自己变成可爱玩偶,搭配个性配饰,装进精美包装盒。你准备好了吗?OpenAI又一次吸引了公众的关注。最近,社交媒

    1小时前
    00
  • ​前端开发者的 Kotlin 之旅:理解 Gradle关键文件与目录

    作为一名前端开发者迈入 Kotlin 世界的第一步,你会立即遇到与前端构建工具截然不同的 Gradle 构建系统。初次打开 Kotlin 或 Android 项目时,各种 gradle 相关文件和目录可能会让人感到困惑。本文将从前端开发者的

    1小时前
    00
  • 淘宝海外暴走的背后,是"川普悖论"的实际代价

    淘宝海外"暴走"之前美区 TikTok 被封,泼天流量给到了海外小红书。这次川普搞"对等关税",泼天流量给到了海外淘宝。由于这次"对等关税",祸害的不止是美国人民,影响面覆盖全球,

    1小时前
    00
  • OPC UA还是MQTT???

    我们现在的工业自动化领域,对于设备的互联互通越来越多了,另外,IT和OT的融合也越来越密切了。这种融合本身也是大势所趋,而MQTT作为物联网普遍的通讯协议,由于其简单性大量应用。OPC UA作为工业自动化领域的开放式协议,也是目前来说仍是主

    1小时前
    00
  • 混淆重定向SVG钓鱼邮件技术分析

    近日,作者收集到一批恶意钓鱼邮件,大多数由html,pdf,svg的文件格式上传发送至企业邮箱中,通常情况下大多数恶意邮件都会被outlook,gmail等放入垃圾箱,但这些附件却顺利进入到了企业员工的收件箱中,经过作者研究是进行了混淆技术

    59分钟前
    00
  • 万能的前向声明碰到他竟然不行了

    您好,我是昊天,国内某头部音频公司的C++主程,多年的音视频开发经验,熟悉Qt、FFmpeg、OpenGL。如果你对这些感兴趣,欢迎关注我的公众号最近遇到一个问题,可简化如下:汽车类存在一个引擎实例,由于汽车内只有一个引擎且不涉及共享所有权

    53分钟前
    00
  • AI“捕风捉影”:深度学习如何让网络事件检测更智能?

    AI“捕风捉影”:深度学习如何让网络事件检测更智能?在现代网络运维中,光靠人肉盯着日志是不现实的。每天大量的网络请求、异常流量、错误日志,如洪水般涌来,靠人工筛选基本是“竹篮打水”。但深度学习的加入,让网络事件检测不再只是简单的规则匹配,而

    44分钟前
    00
  • Java Maven Settings配置参考

    介绍快速概览settings.xml文件中的 settings 元素包含用于定义以各种方式配置Maven执行的值的元素,如pom.xml,但不应绑定到任何特定项目或分发给受众。这些值包括本地仓库位置、备用远程仓库服务器和身份验证信息。set

    39分钟前
    00
  • Django 跨域访问POST请求需预先发送option请求问题处理方案

    跨域访问POST请求需预先发送option请求问题处理方案实践环境Win 10Python 3.5.4Django-2.0.13.tar.gz官方下载地址:.0.13tarball问题描述使用POST请求访问Django后端API时自动

    36分钟前
    00
  • Django 文件导入实现方案

    开发环境Win 10Python 3.5.4Django-2.0.13.tar.gz 官方下载地址:.0.13tarballvue 2.5.2djangorestframework-3.9.4下载地址:实现思路1、 上传文件2、 获取上

    34分钟前
    00
  • EasyDoc文档解析API,实测体验分享

    最近在处理一批论文数据,之前经常用到的一些开源工具比如Mineru,有了预算之后我们调研一些闭源解析服务,虽然付费但是如果解析质量比较好的话,我们也是可以接收的。其中有一个工具是EasyDoc,下面给大家分享一下EasyDoc文档解析API

    30分钟前
    00
  • Linux:简单指令(二)

    要么对内容要么对属性操作决定路径开始定位文件,也可以相对路径家目录man ~~ 1查询具体命令我们可以man man 可以看man 的描述我们可以man 数字 ~~ 可以从上到下查询2查询仿命令3查询具体接口 man 3 printfm

    24分钟前
    00
  • 字符串系列一>最长回文子串

    题目: 链接: link在这里插入图片描述解析: 这里是引用代码:代码语言:javascript代码运行次数:0运行复制class Solution {public String longestPalindrome(String s) {c

    22分钟前
    00
  • 【Git#1】初识 git(配置 & 基本认识 & 文件操作)

    一、前言在工作或学习时经常遇到这样的情况:我们在编写各种文档时,为了防止文档丢失,更改失误,失误后能恢复到原来的版本,不得不复制出一个副本,比如:“报告-v1”“报告-v2”“报告-v3”“报告-确定版”“报告-最终版” 每个版本有各自的内

    15分钟前
    00
  • 【文件操作与IO】详细解析文件操作与IO (一)

    一. 什么是文件1. 认识文件先来认识狭义上的文件(file)。针对硬盘这种持久化存储的IO设备,当我们想要进行数据保存时,往往不是保存成一个整体,而是独立成一个个的单位进行保存,这个独立的单位就被抽象成文件的概念,就类似办公桌上的一份份

    10分钟前
    00
  • 又被AI淘汰了!一句话生成自动化操作

    现在谁还手动操作浏览器,应该学会让AI做你自己的打工人,释放自己的双手去,你的手应该是用来摸鱼而不是做这些费时并且无聊的操作代码语言:javascript代码运行次数:0运行复制地址:现在我们只需要通过人类的语言也就是自然语言告诉AI,帮

    3分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信