我正在实施一个必须接受所有url请求的项目

时间:2018-08-17 21:01:17

标签: javascript html

例如,

如果基本URL是example.com,它以index.html作为响应,而请求是example.com/path,则我必须返回index.html作为响应。我知道使用网络框架可以解决此问题,但我想使用纯JavaScript来实现。

编辑: 如果基本网址是example.com,那么如果我在浏览器网址栏中输入example.com/hello或example.com/world或example.com/anything,则必须返回相同的index.html页面。

1 个答案:

答案 0 :(得分:1)

我建议阅读有关https://medium.com/@pshrmn/single-page-applications-and-the-server-32a23d67936的单页应用程序(SPA)

简而言之,您需要知道example.com正在运行哪种类型的Web服务器。由此,您需要配置所有流量以点击您的example.com/index.html

例如,如果它是Apache2,则可以创建一个.htaccess文件,其中包含以下内容

<form id="sendEmailForm-1" method="post" action="">
  <input type="text" name="client-name" class="form-control" autofocus="" placeholder="What is your name?" required="">

  <div class="space-15"></div>
  <input type="text" name="client-email" class="form-control" autofocus="" placeholder="What is your email?" required="">

  <div class="space-15"></div>
  <input type="text" name="email-subject" class="form-control" autofocus="" placeholder="Give it a good subject" required="">

  <div class="space-15"></div>
  <textarea type="text" name="email-body" class="form-control textarea-lg" autofocus="" placeholder="What is your question?" required=""></textarea>

  <div class="space-15"></div>
  <button type="submit" class="btn btn-lg btn-dark btn-block" id="send-ask-message">Send!</button>
</form>

如果是nginx,那么您将使用类似

的配置
RewriteEngine On
# set the base URL prefix
RewriteBase /
# for requests for index.html, just respond with the file
RewriteRule ^index\.html$ - [L]
# if requested path is not a valid filename, continue rewrite
RewriteCond %{REQUEST_FILENAME} !-f
# if requested path is not a valid directory, continue rewrite
RewriteCond %{REQUEST_FILENAME} !-d
# if you have continue to here, respond with index.html
RewriteRule . /index.html [L]

但是仍然有很多松散的地方,以便在此处给出准确的答案