在Express中混合使用通配符和参数

时间:2016-09-07 06:14:06

标签: express url-routing

我有Minecraft Overviewer生成的网页内容:

public class MainActivity extends AppCompatActivity { String url = "http://ec2-52-40-135-171.us-west-2.compute.amazonaws.com/shop-cart/backend/web/api/category"; private ExpandListAdapter ExpAdapter; private ExpandableListView ExpandList; ArrayList<Group> list; ArrayList<Child> ch_list; Group gru; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ExpandList = (ExpandableListView) findViewById(R.id.exp_list); list = new ArrayList<Group>(); makejsonobjreq(); ExpandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { Toast.makeText(MainActivity.this, list.get(i) + "", Toast.LENGTH_SHORT).show(); return false; } }); ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { Toast.makeText(MainActivity.this, "" + ExpAdapter.getChild(i, i1), Toast.LENGTH_SHORT).show(); return false; } }); } private void makejsonobjreq() { JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e("RES", String.valueOf(response)); try { JSONObject jo = response.getJSONObject("data"); Log.e("data RES", jo.toString()); Log.e("dataLENGTH", String.valueOf(jo.length())); //CHNGE NEEDED ==> REPLACE 2 WITH jo.length(); for (int i = 0; i < 2; i++) { gru = new Group(); gru.setName(jo.getJSONObject(String.valueOf(i + 1)).getJSONArray("parent").getJSONObject(0).getString("name")); Log.e("gru", gru.getName()); ch_list = new ArrayList<Child>(); JSONArray ja = jo.getJSONObject(String.valueOf(i + 1)).getJSONArray("child"); Log.e("child ARRAY", String.valueOf(ja)); Log.e("child LENGTH", String.valueOf(ja.length())); for (int j = 0; j < ja.length(); j++) { Child ch = new Child(); ch.setName(ja.getJSONObject(j).getString("name")); ch_list.add(ch); Log.e("CHILD NAME", "VAL OF J " + j + " " + ja.getJSONObject(j).getString("name")); } gru.setItems(ch_list); list.add(gru); } ExpAdapter = new ExpandListAdapter(MainActivity.this, list); ExpandList.setAdapter(ExpAdapter); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(jsonObjectRequest); } }

我有一个带有express / nodejs的非常简单服务器。这是我的app.js:

/home/mc/backups/servername/latest/overviewer

我包含的代码多于运行的代码,所以你可以看到我的意图,以防这是一个xy问题。我想要路由静态文件,但我认为我不能使用var express = require('express'); var app = express(); //----------------------------------------------------------------------------+ // Each server's root points to the latest overviewer page | //----------------------------------------------------------------------------+ app.get('/minecraft/:server/*', function(req, res) { console.log('HELLO?'); res.send('Finally some luck!'); /* var server = req.params.server, file = req.params[0] ? req.params[0] : 'index.html', dir = '../backups/' + server + '/latest/overviewer'; res.sendFile(file, { root: dir }); */ }); app.use(express.static('www')); app.listen(80, function () { console.log('Web server listening on port 80'); }); ,因为我希望URL的映射方式与我的文件结构略有不同(并且基于服务器名称)。

那么我的简单服务器有什么问题?当我尝试导航到express.static时,我看到一个白页说mysite.com/minecraft/isopre。如果我从第7行路由的字符串末尾删除Cannot GET /minecraft/isopre,我会看到*。但是我想要那里的明星,所以我可以将Finally some luck!映射到mysite.com/minecraft/isopreindex.html

那么这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

为了执行理想的操作,我建议您使用符号表示正则表达式:

app.get('/minecraft/:server/:file?', function(req, res, next) {
    if('undefined' != typeof req.params.file && req.params.file) {
        var file = req.params.file;
    }

    if('undefined' != typeof req.params.server && req.params.server) {
        var server = req.params.server;
    }

});

在这种情况下:如果没有通配符,则file变为可选,node.js不会使整个应用程序失败。

因此,如果req.params.file变量未定义,则可以提供index.html文件。