如何在Sails JS中创建自定义路由

时间:2016-08-19 10:12:30

标签: javascript sails.js

我正在尝试在Sails中创建自定义路由,并根据他们的文档,如果我在/// <summary> /// Opens a local file or url in the default web browser. /// Can be used both for opening urls, or html readme docs. /// </summary> /// <param name="pathOrUrl">Path of the local file or url</param> /// <returns>False if the default browser could not be opened.</returns> public static Boolean OpenInDefaultBrowser(String pathOrUrl) { // Trim any surrounding quotes and spaces. pathOrUrl = pathOrUrl.Trim().Trim('"').Trim(); // Default protocol to "http" String protocol = Uri.UriSchemeHttp; // Correct the protocol to that in the actual url if (Regex.IsMatch(pathOrUrl, "^[a-z]+" + Regex.Escape(Uri.SchemeDelimiter), RegexOptions.IgnoreCase)) { Int32 schemeEnd = pathOrUrl.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal); if (schemeEnd > -1) protocol = pathOrUrl.Substring(0, schemeEnd).ToLowerInvariant(); } // Surround with quotes pathOrUrl = "\"" + pathOrUrl + "\""; Object fetchedVal; String defBrowser = null; // Look up user choice translation of protocol to program id using (RegistryKey userDefBrowserKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\" + protocol + @"\UserChoice")) if (userDefBrowserKey != null && (fetchedVal = userDefBrowserKey.GetValue("Progid")) != null) // Programs are looked up the same way as protocols in the later code, so we just overwrite the protocol variable. protocol = fetchedVal as String; // Look up protocol (or programId from UserChoice) in the registry, in priority order. // Current User registry using (RegistryKey defBrowserKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\" + protocol + @"\shell\open\command")) if (defBrowserKey != null && (fetchedVal = defBrowserKey.GetValue(null)) != null) defBrowser = fetchedVal as String; // Local Machine registry if (defBrowser == null) using (RegistryKey defBrowserKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\" + protocol + @"\shell\open\command")) if (defBrowserKey != null && (fetchedVal = defBrowserKey.GetValue(null)) != null) defBrowser = fetchedVal as String; // Root registry if (defBrowser == null) using (RegistryKey defBrowserKey = Registry.ClassesRoot.OpenSubKey(protocol + @"\shell\open\command")) if (defBrowserKey != null && (fetchedVal = defBrowserKey.GetValue(null)) != null) defBrowser = fetchedVal as String; // Nothing found. Return. if (String.IsNullOrEmpty(defBrowser)) return false; String defBrowserProcess; // Parse browser parameters. This code first assembles the full command line, and then splits it into the program and its parameters. Boolean hasArg = false; if (defBrowser.Contains("%1")) { // If url in the command line is surrounded by quotes, ignore those; our url already has quotes. if (defBrowser.Contains("\"%1\"")) defBrowser = defBrowser.Replace("\"%1\"", pathOrUrl); else defBrowser = defBrowser.Replace("%1", pathOrUrl); hasArg = true; } Int32 spIndex; if (defBrowser[0] == '"') defBrowserProcess = defBrowser.Substring(0, defBrowser.IndexOf('"', 1) + 2).Trim(); else if ((spIndex = defBrowser.IndexOf(" ", StringComparison.Ordinal)) > -1) defBrowserProcess = defBrowser.Substring(0, spIndex).Trim(); else defBrowserProcess = defBrowser; String defBrowserArgs = defBrowser.Substring(defBrowserProcess.Length).TrimStart(); // Not sure if this is possible / allowed, but better support it anyway. if (!hasArg) { if (defBrowserArgs.Length > 0) defBrowserArgs += " "; defBrowserArgs += pathOrUrl; } // Run the process. defBrowserProcess = defBrowserProcess.Trim('"'); if (!File.Exists(defBrowserProcess)) return false; ProcessStartInfo psi = new ProcessStartInfo(defBrowserProcess, defBrowserArgs); psi.WorkingDirectory = Path.GetDirectoryName(defBrowserProcess); Process.Start(psi); return true; } 中添加以下内容:

config/routes.js

请求将由'post /api/signin': 'AuthController.index', 中的index操作处理,但似乎根本不起作用。当我在邮递员中尝试AuthController时,我什么也得不回来。

请注意,我已在/api/login添加了restPrefix: '/api'。请注意我使用的是Sails 0.12.x

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

由于您指向一个带有方法索引的控制器,您需要将其添加到控制器并从那里发送JSON响应(因为您使用的是post)。这是一个简单的例子

配置/ routes.js

model <- glm(HomeTeamScore ~ AwayTeamScore + HomeTeam + AwayTeam,
             family:poisson(link = "log"), data = results)
summary(model)

API /控制器/ AuthController.js

Day       Date    HomeTeam    AwayTeam HomeTeamScore AwayTeamScore   Competition
1 Friday 2015-08-14 Aston Villa  Man United             0             1 PremierLeague
2 Monday 2015-08-10   West Brom    Man City             0             3 PremierLeague
3 Monday 2015-08-17   Liverpool Bournemouth             1             0 PremierLeague
4 Monday 2015-08-24     Arsenal   Liverpool             0             0 PremierLeague
5 Monday 2015-09-14    West Ham   Newcastle             2             0 PremierLeague
6 Monday 2015-09-28   West Brom     Everton             2             3 PremierLeague

更新

由于您已经拥有上述内容,可能是由您的蓝图引起的。

蓝图快捷方式路径

  

默认情况下,新Sails应用程序会激活快捷方式路由,并且可以   通过将'post /api/signin': 'AuthController.index', 设置为module.exports = { index: function(req, res) { var id = req.param('id'); if(!id) { return res.json(400, { error: 'invalid company name or token'}); } /* validate login..*/ res.json(200, {data: "success"}; } } 来关闭   通常在/config/blueprints.js。

     

Sails为任何控制器/模型对创建快捷方式路径   相同的身份。请注意,对类似操作执行相同的操作   RESTful /快捷方式路由。例如,Sails在加载时创建的sails.config.blueprints.shortcutsfalse路由   POST /userGET /user/create会   通过运行相同的代码进行响应(即使您覆盖蓝图   动作)

从sails蓝图文档中可以说,可能会将关闭快捷方式和删除添加到您添加的前缀。

  1. 可能快捷方式正在寻找控制器以外的其他地方,因此返回404。

  2. 前缀是否已添加到您的蓝图连接路线,因此您需要api/controllers/UserController.js才能访问它?

  3. 注意

    我无法在我的计算机上复制您的问题,因为它的工作正常。但我已关闭所有蓝图设置。

    api/models/User.js