404包括javascript

时间:2012-08-10 09:26:46

标签: javascript node.js pug

我有一个node.js应用程序在访问localhost / user / signup时加载注册表单:

html
  head
    script(type='text/javascript',src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
    script(type='text/javascript',src='javascripts/validator.js')
    script(type='text/javascript',src='javascripts/signupValidation.js')
  body
    p(id='error', style='display:none;')#{error}
    form(id='signup',method='post',action='/signup')
      label(for='firstname') Firstname
      input(type='text', name='firstname')
      label(for='lastname') Lastname
      input(type='text', name='lastname')
      label Username
      input(type='text', name='username')
      label Password
      input(type='password', name='password')
      label Password again
      input(type='password', name='password2')
      label Email
      input(type='email', name='email')

      input(type='submit',value='Sign up', onclick="")

但是当我访问该页面时,服务器控制台记录获取JavaScript文件时出现404错误,它正常工作,直到我将注册页面从localhost/signup更改为localhost/user/signup时才引起共鸣:

app.get('/signup',user.signupForm);

为:

app.get('/user/signup',user.signupForm);

1 个答案:

答案 0 :(得分:0)

从您的问题我假设您没有移动JavaScript文件,因此您的目录结构如下所示:

localhost
|- user
|-- signup
|- javascripts

在这种情况下,必须更改JavaScript文件的相对路径才能指向正确的文件:

script(type='text/javascript',src='../javascripts/validator.js')
script(type='text/javascript',src='../javascripts/signupValidation.js')

请记住,如果您不使用绝对路径,则路径始终被视为相对于当前目录的 ,因此在您的情况下,系统正在localhost/user/javascripts/中搜索JavaScript文件,这不存在,从而导致404错误!